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?
- Create a new file: archive.php
- Copy and paste everything from index.php to archive.php
- Save archive.php
- In the archive.php file, change the_content to the_excerpt.
- Save the archive.php file again.
By creating an archive.php file and changing it to make it different from index.php, you are customizing 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 a category.php file and customize it.
Step 2
- Create a new file: search.php
- Copy and paste everything from archive.php to search.php
- Save and you’re done.
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
- Create two new files: page.php and single.php
- Copy and paste everything from index.php to page.php and single.php. (For now, page and single should be the same.)
- Save page and single. Close index. Close single.
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 page links.
Save the page.php file and close it.
Step 5
Clicking 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
- You created four new files or sub templates: archive.php, search.php, page.php, and single.php.
- The archive.php and search.php templates are the same.
- Pages (different from posts) don’t have categories attached to them. They also don’t have Next and Previous page links.
- Single.php can’t display the number of comments link (called for by the comments_popup_link() function) and it doesn’t use posts_nav_link() to call for navigation links.
0 comments:
Post a Comment