Digital Base – Blog » rss

Wordpress : Seperate RSS feed for Category / Author / Tag

Posted by Gijs Nelissen in Programming

As you may have read we migrated this blog to a wordpress one. Yeahyeah, we are not officially wordpressified.

My impression of the core

The default wordpress core holds alot of use’eable functions, but it took me a while to figure out how the thing worked. You need to know that wordpress is not really written very object oriented, so basically its a collection of functions although some “under the hood” core functionality is object oriented.

If i was not convinced wordpress is a serious project with a great future, i wouldn’t have chosen it, so let’s not start the “code quality” discussion.

The problem

Something i was missing after the default drupal installation was an easy way to retrieve the related rss feed based on the page the visitor was accessing. If a visitor was on the “all posts by author X” page, the linked rss feed should only hold those posts. Same for category pages and tag pages.

Getting the information of a tag, author of category is very easy. There are some built in functions like get_category, and get_category_feed_link that will give you this information. The downside is, this information is basically only available inside “the loop“.

The solution

To get the information about the selected category, author or tag i had to jump a few hoops. It’s a combination of core functions and some minor custom code where i had to retrieve the current tag from the queried variables.

Then I created a custom method that returns the most related feed based on the page request. The method will check what type of page the visitor is accessing (category, tag, author or main page) and return the appropriate rss feed.

/**
 * This method will give you the most related rss feed based on the current page request
 *
 * @param $type
 * @return string (feed url)
 */
function get_feed_url($type = "rss2_url") {
 
  if (is_category()) {
    $current = get_the_category();
    $current = $current[0]->cat_ID;
    return $url_rss = get_category_feed_link($current,$type);
  }
 
  if (is_author()) {
    $current = get_query_var('author_name');
    $current = get_userdatabylogin($current);
    $current = $current->ID;
 
    return get_author_feed_link($current,$type);
  }
 
  if (is_tag()) {
    $current = get_query_var('tag');
 
    $formaturl = "/feed/";
    switch ($type) {
      case "rss_url":
        $formaturl = "/rss/";
      break;
      case "atom_url":
        $formaturl = "/atom/";
      break;
 
    }
    return get_bloginfo("url") . "/tag/" . $current . $formaturl;
  }
 
  return get_bloginfo($type);
}

Examples

get_feed_url("rss2_url"); // on page /blog/
 
//returns : http://www.digitalbase.eu/blog/feed/
 
get_feed_url("atom_url"); // on page /blog/category/digitalbase/
 
//returns : http://www.digitalbase.eu/blog/category/digitalbase/feed/atom_url/
 
get_feed_url("rss_url"); // on page /blog/tag/symfony
 
//returns : http://www.digitalbase.eu/blog/tag/symfony/rss/

Enjoy