Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this second video we look at the index.php file and common code it contains.
-
0:00
As we saw in the template hierarchy video,
-
0:03
the index.php serves as the default backup for all other templates.
-
0:08
So, if you do not have other templates set up,
-
0:12
they will default to using the index.php template.
-
0:16
For this reason, you'll usually want to keep the index.php file fairly generic,
-
0:22
unless you're building out all of the other templates as well.
-
0:27
To briefly review, if we take a look again at the template hierarchy,
-
0:32
we could see that we have custom options along the left.
-
0:36
They get more general and
-
0:37
finally end with the index.php as the fallback for all other templates.
-
0:45
If we come in to the code for our theme and
-
0:48
opened up the index.php file, we can see that it's pretty simple.
-
0:54
We have a call to the get header and
-
0:55
footer, which we'll look at more in a minute.
-
0:58
But it should be pretty obvious that that's calling in the header and
-
1:01
the footer files.
-
1:03
We have a bunch of html markup.
-
1:07
Then we have the loop, starting with our if and while statement.
-
1:12
The content that displays for each post or page, the title, and
-
1:17
the content, and then we close out our loop.
-
1:20
We then pull in the sidebar and close out the rest of our markup.
-
1:26
This is a good example of a common generic index.php file.
-
1:31
Because we don't know potentially what page on the site this will be used for,
-
1:35
we have something very generic that's basically just the title and the content.
-
1:41
One thing we could do though here, is instead of just ending the if statement,
-
1:46
we can add an else statement that would then allow us to
-
1:50
put a backup message in case there was no content to display.
-
2:03
For example, we could copy this above.
-
2:06
[BLANK_AUDIO]
-
2:23
Now, what this would do,
-
2:25
is if there's no content, it would give them a message explaining what's going on.
-
2:30
While this may or may not be the best message possible,
-
2:33
it is an example of how you can begin to customize and make this template more
-
2:38
flexible by providing feedback if the content we're looking for is not there.
You need to sign up for Treehouse in order to download course files.
Sign up