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.

27 Comments

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

  1. Posted 4 years, 9 months ago // Permalink

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

  2. Posted 4 years, 9 months ago // Permalink

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

  3. Posted 4 years, 9 months 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 4 years, 9 months 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 4 years, 9 months ago // Permalink

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

  6. Posted 4 years, 8 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 4 years, 8 months 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 4 years, 8 months ago // Permalink

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

  9. Posted 4 years, 7 months 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 4 years, 7 months 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 4 years, 7 months 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 years, 7 months ago // Permalink

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

  13. Posted 4 years, 6 months ago // Permalink

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

  14. Posted 4 years, 6 months ago // Permalink

    Compromise: The menu widgets should be make draggable so that everbody can style it themselves, like the Post and Pages.

  15. Posted 4 years, 5 months ago // Permalink

    this is really a great post, i just installed the wordpress on my new domain techono.com and it was very difficult job to add categories of a beginner like me, i was very confused to add category using the wordpress admin page. but this plug in made everything very easy for me. thanks again for sharing this

  16. Posted 4 years, 3 months ago // Permalink

    nice plugin.. i have to run it on my WP blogs…

  17. Posted 4 years, 3 months ago // Permalink

    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.

  18. Posted 4 years, 2 months ago // Permalink

    A wonderful plugin that I know I’ll be using again – thank you!

  19. Posted 3 years, 11 months ago // Permalink

    thanks for more usefull information, still learning so this was very helpfull.

  20. toninton
    Posted 3 years, 10 months ago // Permalink

    You saved my life.. Thanks a lot!

  21. Posted 3 years, 9 months ago // Permalink

    This plugin is awesome! But it returns an error message in my version of worpress (headers already sent) or something like that. Can you update this plugin? I can’t find anything else like it, and my blog would be so much better if I could use it.
    Thank you!!
    Liat

  22. Menno
    Posted 3 years, 8 months ago // Permalink

    Very nice. Works like a charm. Saves me a lot of work. Thanks

  23. Posted 3 years, 6 months ago // Permalink

    Compromise: The menu widgets should be make draggable so that everbody can style it themselves, like the Post and Pages.

  24. Posted 3 years, 2 months ago // Permalink

    thanks for this awesome plugin

  25. stef
    Posted 2 years, 5 months ago // Permalink

    Sorry, I’m new at this. This is what if done. I’ve downloaded the file, saved it as a .php and copied it at the plugins folder. then I returned to my wordpress page, actived the plugin. But now I don’t know how to use it. How can I add my categories to the menu??? Please help me!

  26. Posted 2 years, 3 months ago // Permalink

    Now its not working, right ?

  27. Posted 1 year, 8 months ago // Permalink

    Works great, exactly what I needed.

13 Trackbacks/Pingbacks

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

  2. [...] Add Categories to Menu Plugin → [...]

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

  4. [...] 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. [...] Add Categories to Menu [...]

  7. [...] 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. [...] Add Categories to Menu [...]

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

  11. [...] of the themes now a days use categories as top menu. If your theme doesn't support that use this: http://www.alistercameron.com/2009/1…tegories-menu/ We Design Resources | Mobile Prices Reply With Quote + Reply to Thread [...]

  12. [...] somatica Member Posted 2 weeks ago # Hi! I am using the Grid Portfolio theme it is really nice but I have a problem. I need to put the categories in the top menu but my theme does not support menus. I've search in internet and I found a plugin called: add categories to menu. (http://www.alistercameron.com/2009/12/13/wordpress-plugin-add-categories-menu/) [...]

Post a Comment

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

Current day month ye@r *

    • splash.oiopublisher.com
    • www.medialayer.net
    • madmimi.com
    • marketsamurai.com
    • Advertise 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