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 plugin like Related Posts by Mark Ghosh http weblogtoolscollectioncomarchives20040608contextualre

Listing 17-14. Saving the First Category from the Post in sidebar-c.php <?php

$categories = get_the_category(); $cat = $categories[0]->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 <?php

$categories = get_the_category(); $cat = $categories[0]->cat_ID;

$posts = get_posts('numberposts=10&category=' . $cat);

<div id="sidebar-b"> <h2><?php _e('Other articles'); ?></h2> <ul class="sidebar-b-sidelists"> <?php foreach($posts as $post) : ?> <li><a href="<?php the_permalink(); ?>"> <?php the_title(); ?></a></li> <?php endforeach; ?> </ul>

Next, add a list of links in the same category to the sidebar, using the WordPress template tag wp_get_links.

Listing 17-16 shows the complete code for sidebar-c.php. As with the get_posts call, if you pass the variable containing the current category as a parameter to the function, the function will output a list of links from that category.

Listing 17-16. Complete sidebar-c.php <?php

$categories = get_the_category(); $cat = $categories[0]->cat_ID;

$posts = get_posts('numberposts=10&category=' . $cat);

<div id="sidebar-b"> <h2><?php _e('Other articles'); ?></h2> <ul class="sidebar-b-sidelists"> <?php foreach($posts as $post) : ?>

<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul>

<h2><?php _e('Resources'); ?></h2> <ul class="sidebar-b-sidelists"> <?php wp_get_links($cat); ?>

<h2><?php _e('Contribute'); ?></h2> <ul class="sidebar-b-sidelists"> <?php wp_register(); ?> <li><?php wp_loginout(); ?></li> </ul>

Although this function can take a long list of parameters specifying how many links are displayed, what information from each link is output, and so on, it is easier to use the Link Categories tab of the Links page, shown in Figure 17-23, to control the output. The values and options you set there are used by wp_get_links.

Figure 17-23. Link category administration

Was this article helpful?

0 0
Blogging For Business

Blogging For Business

Download this Guide And Discover How Start And Run Your Very Own Successful Business Blog. Always Wanted To Start Your Own Blog?

Get My Free Ebook


Post a comment