Alister Cameron // Blogologist

Changing the world. One blog(ger) at a time.

WordPress plugin: Add Categories to Menu

If you’re using WordPress as a CMS, you may have set up static pages for both a front page (e.g. “Home”) and a page to list your latest blog posts (e.g. “Blog”).

I did that today for a client site and immediately realized that in the site’s main menu the Blog link would look a lot better if it had a submenu containing all the blog categories. (I didn’t want nor did I need a completely separate category menu.) So I threw together this little bit of code that takes care of just that:

<?php
if ( !function_exists('add_blog_cats_to_menu') ) {
    function add_blog_cats_to_menu($str) {
        $cats = wp_list_categories('title_li=&echo=0');
        $title = get_the_title(get_option('page_for_posts'));
        return str_replace("title=\"$title\">$title</a>", "title=\"$title\">$title</a><ul>$cats</ul>", $str);
    }
    add_filter('wp_list_pages', 'add_blog_cats_to_menu', 1);
}
?>

For you programmers out there, this is just a slightly intelligent way to inject the output of wp_list_categories() into the output of wp_list_pages(), at the right place.

If looking at code scares you I’ve turned it into a plugin. Go to the Admin > Plugins > Add New page in your WordPress installation and search for “add categories to menu”, or download it here (give that file a .php extension instead of .txt and upload it).

As soon as I have a live site sporting this plugin, I’ll update this post with a link, so you can see a live demo.

I’m likely to update this shortly with a configuration page so you can specify categories to exclude, and so forth. Comment below if you urgently need that.

Updates

  • 14 Dec. 2009 – As per Peter’s comment below, I’ve used the echo parameter to avoid output buffering. Silly me! Simpler now.
 Add Me
Sphere: Related Content

23 Tweets

25 Comments

Note: Commenter website links are not no-followed, in case
you were wondering... I believe in rewarding commenters!

  1. Posted 2 months, 3 weeks ago // Permalink

    wp_list_categories has “echo” parameter so I guess you don’t have to use that output-buffer method…

  2. Posted 2 months, 3 weeks ago // Permalink

    Good point, Peter. I didn’t check that. I’ll change it.

  3. Posted 2 months, 3 weeks ago // Permalink

    Hi,

    I’ve been trying to find a fix for my WPMU install. I want to show all the blogs (65) in a dropdown/accordion style. I tried a widget called ListAllblogs, then your plugin. I don’t see to be able to have them behave properly: when the page is loaded, the list is showing, but all expanded. Can’t seem to find out why, and I’ve been searching for days!

    Would you have any idea? Anyone?

    Regards,

    Andre

  4. RavanH
    Posted 2 months, 3 weeks ago // Permalink

    How ingenious! And I like the simplicity too. Suppose the str_replace is faster than any preg_match but it might break after a WP upgrade where the output of wp_list_pages has only even slightly changed.

    Anyway, is it not on WP Extend yet?

  5. Posted 2 months, 3 weeks ago // Permalink

    many thanks, i great idea that works very well¡¡¡

  6. Posted 2 months ago // Permalink

    I agree with you that in main menu the Blog link would look a lot better if it had a submenu containing all the blog categories.I think you have helped us a lot by sharing this very useful tricks and tips with us.

  7. Posted 1 month, 3 weeks ago // Permalink

    For those of us who are relative neophytes in the general area of blogging, this kind of knowledge sharing is something that is rare, and very much appreciated.

    It’s very obvious from the information here that you have had years of experience in all sorts of blogging situations, recommending then the required solutions/applications.

    Sometimes it is hard for a “black belt” in a discipline to teach a beginner. Based on what you have shared here, I don’t think that is going to be a problem.

  8. Posted 1 month, 3 weeks ago // Permalink

    It’s really helpful plugin. I try to take away latest posts from the home page. Thanks for sharing!

  9. Posted 1 month, 1 week ago // Permalink

    Nice blog you got right here. Will bookmark it. I have a knack in gathering wonderful websites online. Most are about comedians, mentalist and a lot more.

  10. Posted 1 month, 1 week ago // Permalink

    Thanks for the useful plug-in, Alister. Do you have any others that will come up through the WP plug-in search?

  11. Posted 1 month ago // Permalink

    Interesting! Thats pretty cool! You don’t mind if I come here more often and read your posts do you? I love to blog but only on good subjects. Like this one for instance! Can’t wait till you post something else.

  12. churro
    Posted 4 weeks, 1 day ago // Permalink

    please, update to ’select some categories to exclude’… you will save my life…..
    many many thanks fot the plugin…

  13. Posted 1 week, 4 days ago // Permalink

    This is great to free up space in the sidebar where I had listed the categories before. Thanks.

11 Trackbacks/Pingbacks

  1. Kramer auto Pingback[...] WordPress plugin: Add Categories to Menu 8 tweets retweet [...]

  2. Kramer auto Pingback[...] Add Categories to Menu Plugin → [...]

  3. [...] See the original post here: WordPress plugin: Add Categories to Menu [...]

  4. Kramer auto Pingback[...] so you can specify categories to exclude, and so forth. Comment below if you urgently need that.Comments About the Author: Alister Cameron is an accomplished web designer and internet marketing [...]

  5. [...] more: WordPress plugin: Add Categories to Menu Comments0 Leave a Reply Click here to cancel [...]

  6. Kramer auto Pingback[...] Add Categories to Menu [...]

  7. Kramer auto Pingback[...] FriendFeed Chuck Reynolds WordPress plugin: Add Categories to Menu – http://www.alistercameron.com/2009... Tuesday from delicious – Comment – Like – [...]

  8. [...] as links in a submenu. It does involve making a php file from downloaded code. Download at: WordPress plugin: Add Categories to Menu __________________ Mail [...]

  9. Kramer auto Pingback[...] Add Categories to Menu [...]

  10. [...] WordPress plugin: Add Categories to Menu (alistercameron.com) AKPC_IDS += "463,";Popularity: unranked [?] Click here to cancel reply. [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*
retaggr

Additional comments powered by BackType

  • Look Who’s Been Here!

  • My Posts in Your In-box!

    Enter your email address here for instant updates in your inbox, whenever I post something new.
    Your email address is in safe hands. Relax!
© Copyright 2009 Alister Cameron. All Rights Reserved Theme // Sitemap // RSS