Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,900 courses, including more Web and personalized recommendations.Start Your Free Trial Now
- View Offline
- Understanding development environment requirements
- Working with existing WordPress content
- Handling standard pages
- Managing full posts
- Coding a basic template
- Adding a custom sidebar
- Working with WordPress functions
- Inserting PHP code in posts
- Including a recordset
- Outputting data
- Linking to dynamic pages
Skill Level Intermediate
With its default settings, WordPress shows the most recent ten posts on the homepage. If you take a look at the archives link, you will see a listing of up to ten archives. The same thing is true of search results. While it's great to have a configurable setting, showing how many posts and archives and search results you can show, I think it's a little bit of a limitation that the same setting applies across the board. In this movie, we are going to look at how you can limit the number of posts displayed on the home page without affecting the rest of your site.
So I'm going to drill into my theme, tpa, and open up the index.php file, which is the main content area, switch to Code view, and I wanted to take just a moment to show you the PHP code that's responsible for outputting those posts. It starts here with the code block on line 4 and continues all the way down to line 23, the endif that you find there. This is known in WordPress speak as the loop. If there are posts, while there are posts, output the content that's found, in this case, in an article tag, including a link to title and an h2 tag, a div of meta content--basically the time it was published--the content itself, and then another div of post metadata, which show tags, categories, and comments.
By default, this loop will display as many posts, up to the number available defined in the WordPress settings. Let me show you where that is. So go to Settings > Reading, and here you'll see blog pages that show at most ten posts. So if we take a look at the site now, if I click on Visit Site, there is one post, there is two posts, and you have just two there. But let's go ahead and search for IP, and here's one, two, three. So it definitely shows more than two and if I had ten results for that, it will show those.
Now, let's look what happens if I change it to 1. Save Changes. Now let's go back to our site, and here we have just the one post on the homepage. That's great. But now, let's search for IP again, and we only have one search result returning. So the value that you set here is really a global setting. I'm going to restore it to 10 and then click Save Changes. Now, let's go back to Dreamweaver where we can put in a little bit of PHP code to limit your posts just on the homepage. And this code will go right after if (have_posts() ) on line 4.
I am going to open up another PHP code block, and put in the function query_posts(); and then within the parentheses, a pair of quotes, and we put in the property showposts= and then the number of posts that you want shown, in this case 1. Now let me close off the PHP function, save this. With the file saved, let's go back and take a look now.
In our site, we now have just the most recent posts, and in our search results, I will search for the same two-letter combination, and now we've got multiple results there. You will find that the query_posts function is very helpful. You can actually use it to limit your post to a specific category or tag, as well as the number. However, you should be aware that because it alters the main loop, it could affect the rest of your page.
To avoid these issues, use the wp_reset_query function after you're done. This code will go after the endif. Let me scroll up a little bit so you can see it better. Another code block, wp_reset_query, and then just an open and close parenthesis and a semicolon. Close the code block, and this page is done. So let me save it. Let's try it out in Dreamweaver.
I'll open up the index.php page from the site root, go into Design view and then Live view, and now I should have just the single post here on the homepage. It looks good. Let's try our search, and I'll enter in a value and then hold down the Ctrl key while I click Search. So I get the search results that follow that link, and here's our multiple search results. Okay. So we have successfully limited our index page to showing just the most recent posts while displaying multiple entries on other WordPress pages throughout the site.
Now that you've got a handle on managing full posts, you are ready to tackle partial posts, or excerpts.