Turn your attention to the sidebar of this site, where you’ll find lists of posts underneath subheadings like “Must Reads,” “Improve Your Blog,” and “Worth a Look.” As you might have guessed, I use categories to control the content of each list, and now we’re going to examine how you can do the same. For the sake of this example, let’s focus on the “Must Reads,” which are controlled by the “Popular” category.
The first step is to categorize each applicable post in an identical and meaningful way—in this case, “Popular.” Keep in mind that it makes no difference how many posts you lump under one category, simply because you’ll establish all display control through your code (which you are about to write).
Once you’ve categorized your articles, the second step is to determine what you want to display and where you want to display it. In this example, the goal is to display a list of popular posts over in the sidebar, so in order to do that, you should open the sidebar.php template file for editing.
Now that you’ve opened the appropriate theme file, it’s time to move on to the third step, which is the coding portion of our show. Here’s the code I used in sidebar.php to generate a list (<ul>, <li>) of popular articles:
<?php query_posts('category_name=Popular&showposts=5'); while (have_posts()) : the_post(); ?><li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?> <?php comments_number('0', '1', '%'); ?></a></li><?php endwhile; ?>
Take a look at the following snippet from the above code:
query_posts('category_name=Popular&showposts=5');
This line tells WordPress to look through its database and fetch the 5 most recent posts from the “Popular” category. Once you’ve acquired the appropriate posts, all you need to do is loop through them, displaying only the information you want. Here’s the code from our example:
<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?> <?php comments_number('0', '1', '%'); ?></a></li>
In this case, I chose to display direct links to the posts, and I also embellished a bit to include the number of comments on each. The most important thing to take away from this is the fact that I could have displayed any piece of information associated with the resulting posts—I just tailored the output to my exact needs.

RSS Feeds
Feed Comment 




Leave Your Comments Below