wordpress hackery for the day – automatically generate category rss links for category archive pages

i’m helping my dad set up a blog for his law office, and he’s decided to focus on a few main category areas and build up audiences for each.

i told him that we could use the category archives that wordpress generates to filter the posts. then we could link to these pages and style them however we want as we design the site as a whole, and people can subscribe to the rss feeds for only the categories they want.

i was not lying, but today when i actually set about to ask the category feeds to play nice with my dad’s theme, it was a little bit harder than it should have been, and i decided to blog about my solution.

the theme has a big friendly “subcribe to our feed!” link, that i like very much, and it uses a file that it calls ‘subscribe.php’ to generate the link, which i also like. for those of you playing along at home, that means that you can just include ‘subscribe.php’ anywhere you want an rss link to show up, and if you ever want to change the way the link works, you only have to update one file. hurray!

for my purposes, however, a different rss link needs to be generated when you are on a category page, and it has to be the right one. one way of doing this would be to make a custom category template for each page, which wordpress lets you do by making pages like ‘category-17.php’ in your theme folder. but that would be annoying, and it would take away the benefit of having only one piece of code to change, which would be even more annoying, not to mention bad practice.

the better way is to add a condition to the ‘subscribe.php’ file that checks to see if the page you are on is a category archive, and then generates the appropriate rss link. that should be easy enough, but i couldn’t find a function that automatically retrieves category-specific rss feeds, and the otherwise convenient fact that there are several different ways to refer to rss feeds (but only one way to refer to a category-specific feed), was giving me trouble.

sigh.

i spent a while searching the wordpress codex to see if someone had already written the code or a plugin for me, because it seems like the kind of thing that would be a relatively common desire. sure enough, i found lots of people who wanted to do this, but only one who seemed to have succeeded, and his solution seemed really wonky to me and i wasn’t sure it would even work in the new version of wordpress.

in many ways, the strength of the wordpress community spoils me for times like this. i can almost always find answers to my codex questions in less than five minutes, so when i can’t, i just assume the answer isn’t out there, which probably isn’t true. in any case, if i can’t find the answer before i get tired of looking, other people probably can’t either, so it seemed like a good opportunity to increase the number of answers in the pool. :)

after a few frustrations with brackets and such that are par for the course, i made it work!

‘subscribe.php’ now uses the following code to generate the RSS link:

" title="Subscribe to this Feed via RSS">Subscribe via RSS

that’s a lot of babbling so i won’t say more unless there are questions.
good night and good luck!

Leave a Reply