I love WordPress. It will do just about anything that you can imagine. And no, it’s not just a blogging platform. It may have started out that way, but now it’s grown into a full fledged Content Management System (CMS). As a matter of fact, I’m using it here on this website. The cool thing about WP, is once you get the hang of “the loop”, it’s really quite easy for a developer to figure out how to bend it to his or her own will. I am by no means a WP expert, nor do I claim to be. I’ve figured out enough to
be dangerous get by and I’m learning new “tricks” everyday. And that, I think is the key. I haven’t hit the wall yet on learning. It’s intuitive enough that you don’t have to be an expert to get started and before you know it, you’ve taught yourself some pretty cool techniques and in the process, you’ve become a WordPress jedi without even trying!
Alllllrighty then, on with the lesson. I’m making a huge assumption that you are at least a bit familiar with WordPress and have a working site up and running either on the web or locally. If not, at least visit WordPress.org and take a look. My plan today is to show you how to display the most current “Post” on a “Page” and have the next three Post Titles (in two different categories) show up in the sidebar. This happens dynamically and adjusts itself every time you create a new post.
First, you’ll need to log into your admin screen and you should be looking at the dashboard. Under “Pages” on the left hand side, create a new Page and leave the contents of that page empty. My page is titled “tutorials”. Next you’ll want to create a couple categories. So under the heading of “Posts” (which is right above “Pages”), click on the word “Categories”. If you don’t see it, hover your mouse over the “Posts” heading and a small triangle will appear to the right. Click that and the box will expand revealing “Categories”. You should now be on the “Categories” page of the WordPress dashboard. If you have not previously created a category, the default “uncategorized” category is automatically created for you. For practical purposes, we’re going to use “html/css” and “wordpress” as our category titles. Where is says “add new category” fill in the name input and click the “add new category” button. Do this for each new category. Now you should see “html/css” and “wordpress” listed along with the “uncategorized” categories.
Now we’re all set. For this to work, you will need to have at least seven published posts. Three should be under the category of “html/css” and three should be under the category of “wordpress”. The final one can be categorized under either. Why do you need seven published posts? Because we want three titles to show up in the sidebar for EACH category. That makes six. And the seventh post will show up in it’s entirety as a post in the main content area. Does that make sense?
Phew, that’s finally out of the way. So let’s hook this thing up! Back at the dashboard, locate “Settings” and find the word “Reading” and click on it. When “reading settings” page loads, change the “Blog pages show at most” setting to one. This ensures that only one post will show at a time. This can be changed later on if you want more than one blog post to show up at a time AND you have more than seven posts.
Back at the dashboard, open “appearance” and click on “editor”. The “edit themes” page will load and in my case, my .css file is open. Make sure you are in your theme (check the pull down menu to the right) and open the sidebar.php file. You’re going to have to poke around a little bit if you’re not using your own hand-coded theme, and find something that looks like this:
This bit of code is telling WordPress to look for all of your posts and arrange them chronologically in an unordered list by the month. We want a total of three titles from our categories. So this needs to be replaced. I hear you saying “but Jason, you said we didn’t need to be a WordPress expert!”. I hear you. And you’re right. So how do you know what to replace that bit of code with? My answer is simple. Visit Template Tags/get posts! We’re going to copy and paste what we want and hardly change anything. I’m telling you, you can find just about everything on the WordPress site. As luck would have it, the very first example is exactly what we are looking for. So get ready, we’re gonna do a little cuttin’ & pastin’. Copy this code:
- global $post;
- $myposts = get_posts('numberposts=5&offset=1&category=1');
- foreach($myposts as $post) :
- <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
- <?php endforeach; ?>
<?php global $post; $myposts = get_posts('numberposts=5&offset=1&category=1'); foreach($myposts as $post) : setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?>
And paste it where that other bit of code was, keeping the ul tags.
Let’s take a look at what is going on here. To be honest, I have no idea. Well, I kinda have an idea. What I’m focusing on is that third line that says $myposts = get_posts . . . Just from being able to read English, I can decipher that I’m going to have to change the numbers in the ‘numberposts’, and category’ area. Right now it says it’s going to show five posts, in category 1. The offset number is correct. If set to one, it will skip the first (or most current) post. If it was set to zero, it would list the most current title but then we would be repeating because the most current post will be displayed in the main content area AND sidbar. We’ll change the ‘numberposts=’ to three and ‘category=’ to 9 in my case. To find your category number you’ll have to navigate back to the “categories” page and where the categories are listed, click on “edit” under the text “html/css” and then in your browser’s address bar you’ll probably have to use your right-arrow key to navigate all the way to the end of that long url. At the very end it will read “ID=somenumber”. That’s the number that is assigned to that category. Finally, you’ll have to copy that bit of code and paste it inside a new set of ul tags and just change the category number to reflect the number of the “wordpress” category. Done!
See it wasn’t THAT scary. With a just a wee-bit of knowledge, we created our own little php snippet that does what WE want it to do. I hope this helps someone and if you have questions, leave a comment.Tags: get_posts, wordpress