Skip to Main Content

go to Indiana University Indiana University


WordPress: Including Category RSS Feed Links Using the Default Category Widget

One of the benefits of administering this blog is that it allows me to find gaps in the WordPress documentation already published to the web and/or find easier ways to do things than what has already been explained.

Today, I’m looking into how to create the category RSS feeds that you can see in our sidebar. This article will discuss how to create the following by editing the default WordPress category sidebar widget:

RSS Feed text links:

Category Text RSS Links

RSS Feed icon links:

Category RSS Image Links

NOTE: This article is intended for individuals who are working with WordPress at an administrative level. A familiarity with PHP is required.

WordPress Version: 2.8.0 and up

Understanding How Category Lists Are Generated

Before we dive into the specifics of how to edit the Category widget, let’s explore how Category links are made.

In the most recent version of WordPress (3.0.1 at the time of this writing), the function that generates categories is called wp_list_categories() (link opens in new window). I will spare you the entire explanation of how the function works (follow the link to go to the WordPress Codex), but we will need a general working knowledge of how we will send it input.

The typical usage is:

<?php wp_list_categories($args); ?>

Where $args is an array that passes display settings to the function.

Dynamic vs. Static Sidebar: Which One Am I Using?

This article focuses on the dynamic sidebar. The easiest way to tell if you are using a dynamic or static sidebar is whether or not you are using widgets in your theme. If you use widgets, you are using a dynamic sidebar. If you aren’t using widgets, you are using a static sidebar. If you are using a static sidebar, you will probably still get some helpful information out of this post, but it’s truly geared toward the dynamic sidebar.

Creating Category RSS Feed Text Links

Let’s start with the easier of the two methods, text-based RSS links.

  1. To begin, you need to find your wp-includes/default-widgets.php file.
  2. Save the current file as wp-includes/default-widgets-OLD.php.

Now that we’ve backed up our work, we can begin looking for the code we need. In the file, you should find an area of text around line 414 that looks like:

/**
 * Categories widget class
 *
 * @since 2.8.0
 */
class WP_Widget_Categories extends WP_Widget {

	function WP_Widget_Categories() {
		$widget_ops = array( 'classname' => 'widget_categories', 'description' => __( "A list or dropdown of categories" ) );
		$this->WP_Widget('categories', __('Categories'), $widget_ops);
	}

	function widget( $args, $instance ) {
		extract( $args );

		$title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Categories' ) : $instance['title'], $instance, $this->id_base);
		$c = $instance['count'] ? '1' : '0';
		$h = $instance['hierarchical'] ? '1' : '0';
		$d = $instance['dropdown'] ? '1' : '0';

		echo $before_widget;
		if ( $title )
			echo $before_title . $title . $after_title;

		$cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h);

		if ( $d ) {
			$cat_args['show_option_none'] = __('Select Category');
			wp_dropdown_categories(apply_filters('widget_categories_dropdown_args', $cat_args));
?>

There are essentially two locations we are interested in in this code chunk. The first is where we will declare our feed link’s name:

$c = $instance['count'] ? '1' : '0';
$h = $instance['hierarchical'] ? '1' : '0';
$d = $instance['dropdown'] ? '1' : '0';

The second is where we modify the $cat_args array:

$cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h);

We will add a $f variable and set it’s value to ‘rss’. We will then add the variable in the $cat_args array to make the links appear.

  1. To create the $f variable, in the code editor, under the line that reads $d = $instance['dropdown'] ? '1' : '0'; type,
    $f = 'rss';
  2. To modify the $cat_args array, immediately after the $h, type:
    , 'feed' => $f

    The line should now read: $cat_args = array(‘orderby’ => ‘name’, ‘show_count’ => $c, ‘hierarchical’ => $h, ‘feed’ => $f);

    Full code listing for the WP_Widget_Categories class. (opens in new window)

  3. Save your file and upload it back to your wp-includes folder.
  4. Refresh your blog front page.

You should see something similar to:
Category Text RSS Links

Creating Category RSS Icon Links

The process for creating RSS links with an icon is very similar to the above process. Instead of settign the $f variable to 'rss', we will set it to the location of the image file and instead of adding 'feed' => $f to the $cat_args array, we will add 'feed_image' => $f.

To complete this section, you will need a small RSS icon. Right click here and choose ‘Save Target As…’ to use the one that we use. You will also have to upload the image to a folder you can link to from your WordPress installation, we stored ours in our wp-content/themes/current_theme/images folder.

Let’s get started.

  1. Find the same code snippets listed above, namely:
    $c = $instance['count'] ? '1' : '0';
    $h = $instance['hierarchical'] ? '1' : '0';
    $d = $instance['dropdown'] ? '1' : '0';

  2. To create the feed variable, after $d, type:
    $f = ‘path_to_your.image’
  3. To modify the $cat_args variable, immediately after the $h, type:
    , ‘feed_image’ => $f

    The line should now read: $cat_args = array(‘orderby’ => ‘name’, ‘show_count’ => $c, ‘hierarchical’ => $h, ‘feed_image’ => $f);

    Full code listing for the WP_Widget_Categories class. (opens in new window)

  4. Save your file and upload it back to your wp-includes folder.
  5. Refresh your blog front page.

You should see something similar to:
Category RSS Image Links

Where To Go From Here

Now that you have modified your default-widgets.php file, you will have to preserve it before upgrading your WordPress installation. I recommend that you keep a copy of the modified file stored somewhere safe. For example, you could save your default-widgets.php file as default-widgets-NEW.php and move it to your desktop or any other location.


16 Responses to "WordPress: Including Category RSS Feed Links Using the Default Category Widget" to “WordPress: Including Category RSS Feed Links Using the Default Category Widget”

  1. WP Plugins Says:

    I just love wordpress and thanks for explaining this info, very valuable to us wp site builders, the widget area customization is probably the most complicated thing in customizing wp.

  2. kkk Says:

    hello im new to this ..this is a nice post but im having problem with adding image

    http://img824.imageshack.us/img824/2403/40289186.jpg

    i get this (image) (post count) rather than image (post count) and when the mouse is over the rss image it shows the image source

    also what do you mean add feed-image?

    this is the code that shows (image) (post count)

    $f = ”;

    $cat_args = array(‘orderby’ => ‘name’, ‘show_count’ => $c, ‘hierarchical’ => $h, ‘feed’ => $f);

    thanks for any help

  3. Tom Mason Says:

    In your $cat_args variable declaration, change the word ‘feed’ to ‘feed-image’ and it should remove the parenthesis from your image.

    Let me know if that helped!

  4. petere Says:

    Hi
    thanks for this entry – I’ve had a problem though- I tried the ‘feed-image’ => $f in the cat-args line, but nothing displays.
    I’ve tried both absolute and relative paths for the image location ..

    I know it works in general because if I use the ‘feed’ => $f, a text link displays ok
    thanks again for your help

  5. Tom Mason Says:

    Peter,
    You’ll have to let me see your code before I can help you troubleshoot.

  6. petere Says:

    Hi Tom
    Thanks for the response
    here’s the relevant code – this one works, but if I change the ‘feed’ => $f to ‘feed-image’ => $f nothing displays, no matter how I define the image source
    (I’ve commented out a last-gasp effort at a full path to the image):

    $c = $instance['count'] ? ’1′ : ’0′;
    $h = $instance['hierarchical'] ? ’1′ : ’0′;
    $d = $instance['dropdown'] ? ’1′ : ’0′;
    $f = ‘rss’;
    /* $f = ‘http://libnet/currentawareness/wp-includes/images/rss.png’; */

    echo $before_widget;
    if ( $title )
    echo $before_title . $title . $after_title;

    $cat_args = array(‘orderby’ => ‘name’, ‘show_count’ => $c, ‘hierarchical’ => $h, ‘feed’ => $f);

  7. Tom Mason Says:

    Peter,
    A couple of problems:

    1) The image link you have in your $f variable is not a true URL. Make that an absolute URL to the image and it should work.
    2) Remove the line that says $f = ‘rss’ and uncomment the line that has your image URL in it (after you’ve made the changes in problem 1).

    That should fix your problem.

    Let me know if that helps!

  8. petere Says:

    Tom,
    This is an internal site, the image link is actually the full url, but even if I use relative urls it has the same effect.
    I’m wondering if it is the ‘feed-image’ that is not working or needs to be invoked?
    Peter

  9. Tom Mason Says:

    Here’s how I would change your code. I did miss one part earlier:

    $c = $instance['count'] ? ’1′ : ’0′;
    $h = $instance['hierarchical'] ? ’1′ : ’0′;
    $d = $instance['dropdown'] ? ’1′ : ’0′;
    $f = ‘http://libnet/currentawareness/wp-includes/images/rss.png’;

    echo $before_widget;
    if ( $title )
    echo $before_title . $title . $after_title;

    $cat_args = array(‘orderby’ => ‘name’, ‘show_count’ => $c, ‘hierarchical’ => $h, ‘feed-image’ => $f);

    There’s a change in the declaration of the variables as well as a change in the $cat_args array. Namely changing $f to have the value of the image’s URL and changing $cat_args to include feed-image instead of feed. Both changes are in bold above.

    If that still doesn’t work, try linking to an RSS feed image that’s hosted on a public site. I don’t imagine that will make a difference, but it could.

  10. petere Says:

    Hi Tom,
    I had been doing what you suggested in your last post. This time though I tried pointing to an image on a public site, as you suggest – still no luck.
    I’m still mystified
    Thanks for your help though
    Peter

  11. RoFox Says:

    Petere – there is a small mistake
    You should change: ‘feed-image’ to: ‘feed_image’ (underline instead of dash)

    Tom – great tutorial!

    I have two questions:
    1 – how to move the RSS image in front of name of the categories?
    2 – how to use a different feed (‘the_syndication_feed’ instead of standard ‘feed’) which I have from FeedWordPress plugin? (I can use it on a page, but I don’t know how to use it inside of widget)

    Regards,
    RoFox

  12. Tom Mason Says:

    RoFox,
    Thanks for pointing out the typo in the article. It’s been fixed.

    I’ll be looking into your questions over the next couple days; just wanted to thank you sooner rather than later.

  13. RoFox Says:

    Hi Tom,
    Did you try to find how to add the RSS at the beginning of category’s name?
    And how about different feed RSS?

  14. Georges Aoun Says:

    Hy Everyone,

    I’ve succeeded in putting the RSS icon beside the category Name,
    But what i want to do is to put Rss icon BEFORE category name.
    Anyone Can Help me.
    thanks

  15. Umida Khikmatillaeva Says:

    HI!
    My question is not about RSS, but I have a question about using WordPress themes for a website design. Can we use WordPress themes for publishing websites at IU and how can we get hosting? Do we have PHP and MSQL at IU, so we can use Wodpress themes?

    Thank you!
    Umida

  16. Tom Mason Says:

    Umida,
    Yes, you can use WordPress at IU. PHP is installed on Webserve, and IU has a MySQL service. Check out the documents at http://webmaster.iu.edu for more information.

Leave a Reply

 

Get Training Tips via RSS Subscribe to Entries feed or Read Comments via RSS Subscribe to Comments feed.