Skip navigation

WordPress Category Feed Links in Your Post Meta Data Section

I’m a huge fan of all the fabulous volunteers on the WordPress Support Forum. Recently, one of them, known as Otto 42 of Ottodestruct.com, solved a huge problem I had, and I want to share his brilliance with all of you.

The challenge was to create a list of feeds by categories in which that particular post is found. In a post meta data section of a blog I’m developing, I wanted to showcase the category feeds for that post, in case people want to follow those particular categories. I could have put them in a list, but I wanted them to be in a sentence, a comma between each category.

WordPress Theme post meta data featuring feed category links in the textOtto came up with a conditional bit of PHP code that requested the categories associated with the post and turned that information into feed links. The feed links have a comma in between each category, and an “and” before the last category to set the category feed links into a sentence, not a list.

$cats = get_the_category();
foreach ($cats as $cat) {
$catlinks[] = '<a href="'
.get_category_rss_link(false,$cat->cat_ID,$cat->category_nicename)
.'">'.$cat->cat_name.'</a>';
}
echo implode(' and ',
array(implode(', ', array_slice($catlinks,0,-1)),
$catlinks[count($catlinks) -1]));

To use this, the code must be placed in a template file within the WordPress Loop, the bit of PHP code that collects information from the database to display your post content. For my use, I put it in a sentence, as shown in this excerpt:

<li><p>Using feeds, you can monitor this entire blog through our
<a href="<?php bloginfo('rss2_url'); >" title="<?php _e('Syndicate this site using RSS'); >">
<?php _e('<abbr title="Really Simple Syndication">RSS</abbr> Feed'); ></a>, 
or the categories associated with this post: 
<?php
$cats = get_the_category();
foreach ($cats as $cat) {
$catlinks[] = '<a href="'
.get_category_rss_link(false,$cat->cat_ID,$cat->category_nicename)
.'">'.$cat->cat_name.'</a>';
}
echo implode(' and ',
array(implode(', ', array_slice($catlinks,0,-1)),
$catlinks[count($catlinks) -1]));
?>. If you would like to monitor the comments...

What this PHP conditional code does is gather the information from the database using the get_the_category template tag. It gathers the category names and inserts them into the link format created using the get_category_rss_link template tag.

It then generates the category feed links with a comma between each category, then recognizes which category is the last in the list and puts an “and” before the last category.

Awesome! Thank you, Otto, and to everyone who gives up so much of your time to help others for free on the WordPress Support Forum!


Site Search Tags: , , , , , , , , , , , , , , , , , , ,
Copyright Lorelle VanFossen

6 Comments

  1. Posted August 7, 2006 at 4:35 am | Permalink

    Hi Lorelle,

    A much easier way to accomplish category feed links is included in my Category LiveBookmarks plugin. CLB creates a contextually relevant set of category feed links that may be included anywhere within a site. When the links are included in the head of the document, they trigger autodiscovery features (like Firefox Live Bookmarks). When the links are included anywhere (not limited to loop) within the body of the document, a set of customizable feed links is displayed. The plugin is flexible and provides plenty of neatness, but currently does not provide an “and” before the last category.. ;)

    Cheers,
    Jeff

  2. Posted August 7, 2006 at 7:29 am | Permalink

    Ah, something to work on, eh? ;-)

  3. Ceirwyn
    Posted November 20, 2006 at 6:14 pm | Permalink

    I guess your next challenge Lorelle should be to make them do feeds for authors.

  4. Posted April 26, 2007 at 10:57 pm | Permalink

    As a follow up, there are feeds for authors in WordPress.

    example.com/authorname/feed/

    You can read more in Blog Herald Feeds and Customizing Your WordPress Blog Feeds and Understanding, Using and Customizing WordPress Blog Feeds.

  5. Posted September 27, 2007 at 11:17 am | Permalink

    Thank you for the script. It really helps.

  6. Posted December 14, 2007 at 11:32 am | Permalink

    Ahh, if only I had known PHP as well then as I do now.

    This will do the same thing, only much better.

    Fixed:

    $cats = get_the_category();
    foreach ($cats as $cat) {
    $catlinks[] = '<a href="'
    .get_category_rss_link(false,$cat->cat_ID,$cat->category_nicename)
    .'">'.$cat->cat_name.'</a>';
    }
    echo implode(' and ',
    array(implode(', ', array_slice($catlinks,0,-1)),
    $catlinks[count($catlinks) -1]));


9 Trackbacks/Pingbacks

  1. [...] In working on the post related to software updates and making sure it was easy to find the category feed JUST for that led me on a “round the world” kind of quest to find a way to put the category feeds in the footer of pages in WordPress. The first thing I tried was this this category livebookmarks plugin, which gives some options to browsers that recognize feeds in the header of the page. It’s interesting, but I may wind up uninstalling. What I HAVE found is this solution which was EXACTLY what I was looking for. [...]

  2. [...] took ages to work this out or find for that matter so here it is plain and simple. Huge thanks to Lorelle@wordpress for the code that broke the drought. Checkout that link for more on this [...]

  3. [...] took ages to work this out or find for that matter so here it is plain and simple. Huge thanks to Lorelle@wordpress and especially Otto42 (as lorelle pointed out, thanx ) for the code that broke the drought. [...]

  4. [...] WordPress Category Feed Links in Your Post Meta Data Section [...]

  5. [...] Despre feed-uri pe categorii la Lorelle. [...]

  6. [...] WordPress Category Feed Links in Your Post Meta Data Section [...]

  7. [...] in Managing Multiple Authors: Customizing the WordPress Author Page with a little help to put the list into a real sentence structure from Otto of Otto on [...]

  8. […] WordPress Category Feed Links in Your Post Meta Data Section […]

  9. […] WordPress Category Feed Links in Your Post Meta Data Section […]

Post a Comment

Follow

Get every new post delivered to your Inbox.

Join 20,687 other followers

%d bloggers like this: