24. Sub-template Files

Follow this WordPress Theme Tutorial Series from the beginning.

To continue with where we left off, you’re going to create more sub-template files like yesterday’s header.php,sidebar.php, and footer.php files.

Now that the index.php file’s divided, it gets even easier.

Step 1
Before you do this step, look at the Sidebar, click on an Archives link. The resulting page doesn't look different from your front page right?
By creating an archive.php file and changing it to make it different from index.php, you arecustomizing the appearance of archive pages.

Now, if you refresh your archive page, it will give you only excerpts, not the full posts.

Why would you want to do this? – to prevent Google from penalizing your blog for having duplicate content. If one of the archive pages and the front page display the same content, that’s duplicate content.

What if you have a private blog? Then, it’s not necessary to distinguish the archive pages from the front page. That’s not to say excerpts aren’t useful for private blogs.

Also – By default, your category pages will look for instructions on how to display content from the archive.php file. If you don’t have an archive.php file, category pages will look for index.php.

If you want the category pages to look different the front page and archive pages, create acategory.php file and customize it.
Step 2
Now, all search results will be returned as excerpts. Without the search.php template file, the search option looks to index.php on how to display search results.

(Optional) You can go back to lesson one to review the hierarchy.
Step 3
Step 4
There’s a difference between a Page and a page remember? The page.php template customizes the look of those special Pages.

First, in page.php, type the following codes under <?php the_content(); ?>:

<?php link_pages(‘<p><strong>Pages:</strong> ‘, ‘</p>’, ‘number’); ?>

and

<?php edit_post_link(‘Edit’, ‘<p>’, ‘</p>’); ?>

Second, remove the postmetadata codes from page.php. Here’s what you should have without postmetadata.


Third, remove the posts_nav_link() or navigation block from page.php.


What just happened?
The first line of codes was for displaying links to sub-pages.


For example, edit your About page. Follow my screenshot below for what to add.


This is useful for when you have to break down one REALLY long page into multiple pages.

For the second line of codes, it’s your administrator-only Edit link.

Your Pages don’t have categories attached to them and you usually don’t want to show a time stamp for them either so that’s why you removed the postmetadata. You also removed the posts_nav_link() codes because the Page pages don’t display Next page and Previous pagelinks.

Save the page.php file and close it.
Step 5Clicking on a post title to read the rest of an entry takes you to the single post view. single.php template handles the appearance of the single post view.

In single.php, type this under <?php the_content() ?>:

<?php link_pages(‘<p><strong>Pages:</strong> ‘, ‘</p>’, ‘number’); ?>

Yes, that is the same line of codes for sub-page links. Did you know you can also break down posts into multiple sub-posts?

Second, in the postmetadata area, remove the <?php comments_popup_link(); ?> function and the <br /> tag before it. Don’t remove the whole postmetadata.

You removed the comments link function because it doesn't work on single post view so there’s no use in having it in the single.php file. Without the number of comments link, there’s only the invisible, administrator-only, Edit link, left after the BR tag. You don’t want to skip-a-line for an INVISIBLE link that only you can see right? That’s why you removed the BR tag.

Third, replace <?php posts_nav_link(); ?> with:

<?php previous_post_link(‘« %link’) ?> <?php next_post_link(‘ %link »’) ?>

On front, archive, category, and search pages, you use the posts_nav_link() function to call for Next page and Previous page links. For the single post view page, there’s no Next page or Previous page link. You use the previous_post_link() and next_post_link() functions to call for the previous and next post links.

Save the single.php file. Go to a single post view page to see the difference in the navigation area.
Lesson Review