Archive for the ‘Code Snippets’ Category

Add Unique “Per Category” RSS Feeds – WordPress

A client came to me the other day with a neat request. He wanted the ability to offer unique RSS feeds for each of his WordPress blog’s categories. His site uses a custom permalink strategy, but I wanted to develop a solution that would work for all WordPress configurations.

The following code will display a unique RSS feed on each “category archive” page within your WordPress blog or site. The feed will contain only the posts within that category. If you have any questions about the code, or if you just like it a lot, leave a comment to let me know.

<?php /* If this is a category archive */ if (is_category()) { ?>
    <span>
    Subscribe to posts in <a href="/wp-rss2.php?cat=<?php echo get_query_var('cat'); ?>"><?php single_cat_title(); ?></a> via RSS
    </span>
<?php } ?>

List Recent Posts on Page or Sidebar – WordPress

Want to list recent posts on a page? Use the code below.

To use it in your sidebar, just make sure your wrap the entire chunk of code in <li class="widget"></li> tags so that it is recognized as an element of the sidebar <ul>. To add a title in the sidebar, wrap it in <h3 class="widget-title">Recent Posts</h3>, where “Recent Posts” is your desired title.

<?php query_posts('showposts=5'); ?>
<ul>
    <?php while (have_posts()) : the_post(); ?>
        <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
    <?php endwhile;?>
</ul>

Questions? Drop ‘em in the comments

List Categories in Sidebar – WordPress

The code below will list categories in the appropriate format for your WordPress sidebar.
For this example, we exclude items from category 10. If you don’t want to exclude any categories, just remove &exclude=10. To exclude a different category, simply change the number 10 to the number of the category that you’d like to exclude.

<li class="widget">
  <h3 class="widget-title">Categories</h3>
  <ul>
    <?php wp_list_cats('sort_column=name&optioncount=1&exclude=10'); ?>
  </ul>
</li>