484 CHAPTER 17 CUSTOMIZING YOUR BLOG S LAYOUT (Web server application)
484 CHAPTER 17 CUSTOMIZING YOUR BLOG S LAYOUT example, you deal with only the first category from the list. I have set my sample blog to use only single categories per post. Listing 17-14 shows this first step. Note It is quite natural to have multiple categories per post. The code would need to be more sophisticated to handle that situation. It may be easier to use a plug-in like Related Posts by Mark Ghosh (http:// weblogtoolscollection.com/archives/2004/06/08/contextual-related-posts-in-wordpress/). Listing 17-14. Saving the First Category from the Post in sidebar-c.php cat_ID; ?> Once you have the category ID, you can use it with another standard template function get_posts. Not surprisingly, this function will get a list of posts. If you pass in the category ID from the last step, via the variable, the function will return a list of posts in the given category. Add an extra parameter to limit the number of posts returned. Once you have that list of posts, you can loop through the list, extracting the link to the post and the post title. Output them as an HTML link in an unordered list. Listing 17-15 shows the completed code with a header and a class style specified or the list. Note that because it uses the category from the current post, this list will also be in the same category. Listing 17-15. Generating the List of Posts in sidebar-c.php cat_ID; $posts = get_posts(’numberposts=10&category=’ . $cat); ?>