Category Posts in Custom Menu (WordPress)


Download

You can get the plug-in and read its documentation here.

Donate










Description

At some point in time, I wanted to display all posts in a category in my menu, without having to drag them there by hand every time. So I created a plug-in that would do that for me. Over time, I’ve extended it to support various parameters and options.

A short while ago, I managed to fix the remaining issues I had with the plugin, such as the custom fields not starting with an underscore _ and thus appearing in the custom fields for posts. That has been fixed now. Also, the plugin is now compatible with the wp_nav_menu_item_custom_fields hook that is not yet present in the WordPress core but allows multiple plugins to add fields to the Appearance > Menus page without causing conflict.

Lastly, the mechanisms I’m using in WordPress just seem a bit fragile. As it turns out it’s not possible to programmatically add posts to the menu and then programmatically give them children of their own. This makes adding the correct CSS classes quite a pain; although appears to work, I’ve reported an issue in the WordPress core where generation of CSS classes does not take into account plugins that remove, replace and add menu items via the wp_nav_menu_objects hook: https://core.trac.wordpress.org/ticket/32918

Regardless of this last issue, I hope users will find the plugin useful, I’m glad to see it still receives positive reviews and it’s being used quite a bit.

58 thoughts on “Category Posts in Custom Menu (WordPress)

  1. Emil

    Hey! Thank you so much for the great plugin!
    It totally took away my pains!

    I know this is your first plugin, and it’s totally great !
    So this is just for your information if you didn’t know it, and so if you will, can get some programmers out there to correct a flaw in the code. (I don’t know how, would have done it if i could:)

    I am using a visual editor theme called headway, wich is awesome when working with wordpress.
    But the code conflicts somehow with headway, so the headway editor cant load the css at all hehe.
    But the plugin works flawless when viewing in my browser πŸ˜€ πŸ˜€ It’s not a big problem, i just wait to activate it to when i publish the site.

    Hay just wanted to thank you Diane!

    Reply
    1. diana Post author

      Hi Emil,

      thank you for your kind words. I’ve taken a look at the Headway website, but I don’t have a license and I will not be paying money just to fix it if it only occurs in Headway. However, if you can be more specific of what the problem is, I’m willing to take a stab at debugging it blindly. Can you do the following for me: In headway, however that works, activate the plugin and go to a page where the problem occurs. Then, save that page (File > Save). This will save the page as .html, including a folder with the same name in which resources like images and CSS are saved. Now open that .html file on your computer and if the error also occurs there, please put everything in a .zip file and e-mail it to me (diana at imyou) and I’ll see what I can do.

      In any case, glad that it was useful for you, even with headway messing it up πŸ˜‰

      EDIT: Via e-mail I notified Emil that I had updated the plugin to version 0.2 after which he let me know that the problems with Headway do not occur anymore.

      Reply
  2. josh

    Thank you so much for this plugin! Real life saver. It was exactly what I needed for my members area. This plugin mixed with wishlistmember is amazing!

    Thanks!
    Josh

    Reply
  3. diana Post author

    Hi Salvo,

    I added a FAQ to the plugin page at WordPress.org, but hadn’t added it to my personal site yet. I’ve just added two possible explanations. Please check whether one of these applies and let me know!

    Kind regards,
    Diana

    Reply
  4. Derek Johnson

    This is a great plugin that has saved me on a couple of occasions already, so thank you for sharing πŸ™‚

    I’m looking to extend it by adding extra HTML using content from custom fields but don’t know where to start. Can you give me any pointers?

    Reply
  5. diana Post author

    Hi Derek,

    thank you for your message! Glad to hear that you find my plugin useful πŸ™‚

    As for extending the HTML: in what way? Do you mean replacing custom placeholders by data (currently done with %post_author and %post_title)?

    I’m willing to extend the plugin, but only if the added functionality benefits everybody. I could try to add support for inserting the data in custom fields, but I’m not sure yet if and how this is possible, because I’ve never used custom fields before.

    If you can be more specific on what you want to do, I’ll see what I can do for you πŸ™‚ In the meantime, you may want to check out lines 131 and 132 in the plugin source.

    Hope this helps.

    Kind regards,
    Diana

    Reply
  6. Derek Johnson

    Thanks for the reply Diana.

    I have a custom field on each post called “image”, and I have made a custom placeholder %post_image. I want to show the image inside the <li>

    I have added $posts[$pkey]->title = str_replace( “%post_image”, $image, $posts[$pkey]->>title);

    which works when $image is set to a string, but when I set it to a custom field using

    $image = get_post_meta($menu_item->db_id, “image”, true);

    it doesn’t work. Please note at this stage I’m only expecting it to give me the path to the image, not the actual image.

    Reply
    1. diana Post author

      Hi Derek,

      haven’t looked into it in detail yet, but I’m pretty sure you should use something like

      $image = get_post_meta($posts[$pkey]->ID, β€œimage”, true);

      This is because $menu_item->db_id refers to the database-id of the menu item, not of the post itself. Menu-items are stored in the same table and have similar attributes as posts, but aren’t the same thing.

      Let me know if that’s it, if not then I’ll look into it next week.

      Kind regards,
      Diana

      Reply
  7. Derek Johnson

    Hi Diana,

    Thanks again for replying. I’m only just getting back to this now – another project jumped the queue.

    Unfortunately that method didn’t work. Sorry I can’t give you more details but I’m a bit lost in the PHP and database.

    Derek

    Reply
  8. diana Post author

    Hi Derek,

    I’ve just updated the plugin to 0.3, the update should appear sometime soon. You can speed things up by uninstalling 0.2 and then re-installing the plugin from WordPress plugin repo. I’ve added support for custom fields.

    Example:
    Add custom field ‘my image’ to a post and give it the value
    ‘http://diana.imyou.nl/wp-content/uploads/2012/01/cropped-skull-island.jpg’ (without all these quotation marks of course)

    Then, under Appearance > Menus, create a new category/tag menu item, save the menu and then set the Post Navigation Label for this item to something like:

    <span class=”titlespan”>%post_title</span><span class=”imagespan”><img src=”%post_my_image” /></span>

    Note that this requires you to use the custom ‘my image’ field *for everything in that category*, because the %post_my_image wildcard returns empty string if the image is not available, which results in a broken image icon in that case.

    I hope this helps. I’d love to hear from you if this solves your problem

    Kind regards,
    Diana

    Reply
  9. Ruben

    Thanks alot for this plugin, really usefull!
    Is there maybe any way to limit the output of the titles?
    I am using post titles as menu in my right menubar but when they are too long it gets kinda ugly.

    hope to hear from you!
    Thanks for the good plugin anyway!

    Reply
    1. diana Post author

      Hi Ruben,

      Right now, there’s no way to clip the length of the post title (or other field) in the menu. Perhaps I’ll add that later.

      In the meantime, you could:

      • use custom fields instead of post titles, and make the custom field equal to the first x characters of the post title, or
      • use CSS to clip the text. Take a look at overflow:hidden; and white-space:pre; for starters

      Hope that helps.

      Kind regards,
      Diana

      Reply
  10. Bjoern

    Hi Diana,

    first, thanks a lot…! Your plug-in works great and I love to use it!

    But I still have one Question:
    While I’m using it right now, I don’t have the chance to customize the url where it is linking to (e.g. in my case: the plug-in links to “…/?p=4” and I would need to customize it into “#postname” because I’m using anchors and I want it to jump to my posts.)

    I was trying to find the answer in your code, but I guess I’m a too bad developer.

    Tnx a lot,
    BjΓΆrn

    Reply
    1. admin Post author

      Hi BjΓΆrn,

      what you’re trying to do is unrelated to my plug-in. If you want to customize the post-URLs, then you should do so via the Settings > Permalinks option in WordPress. My code simply requests the URL that WordPress has set for the post, and copy-pastes this into the menu. (The code does not manually build the links, it merely requests the posts and post data from the database.)

      I hope this answers your question. If not, feel free to ask, or visit WordPress.org and ask there.
      Thank you very much for responding to my plug-in, I hope you will continue to find it useful.

      Kind regards,
      Diana

      Reply
      1. admin Post author

        It is possible! πŸ™‚ It’s not perfect, but if you’d like, here’s the way to do it:

        • By default the menu items are created like so:

          <a href=”[the_post_url]”>[CPCM Post Navigation Label]</a>

        • Since the Post Navigation Label allows HTML, you can change the link by setting the following label:

          </a><a href=”http://www.mysite.com/something/”>%post_title

          Note that the closing tag comes first, so that the final result becomes:

          <a href=”[the_post_url]”></a>
          <a href=”http://www.mysite.com/something/”>%post_title
          </a>

        Note that this results in an empty <a> tag, I cannot guarantee that this does not have unforeseen consequences.

        Reply
  11. Didier

    Hi,

    I’m trying this plugin, but don’t see it working.

    I save menu after adding a Category/Tag to your menu. but nothing appears

    Disable all other plugin (for higher priority. Nothing.

    I use Suffusion theme : a conflict with it ?

    Thanks for help

    Reply
    1. admin Post author

      Hi Didier,

      I’m sorry to hear the plugin is not working.

      Can you be more specific? What is it that you don’t see?

      1) You don’t see my extra checkboxes under Appearances > Menus
      2) You see my extra checkboxes but after saving, you don’t see a difference on the website
      3) Something else?

      Please consider also switching to the default WordPress theme if you feel that the theme may be causing it.

      Kind regards,
      Diana

      Reply
  12. Didier

    Hi Diana
    Thanks for answer.

    I don’t see extra checkboxes under Appearances > Menus

    But when I switch from Suffusion Theme to Twenty Eleven Theme : it works.

    Any idea (I want to keep suffusion theme)

    Thanks

    Reply
    1. admin Post author

      Hi Didier,

      it is definitely the theme that prevents you from seeing these changes.

      There is nothing I can do about it: The Suffusion theme does not implement the wp_edit_nav_menu_walker hook. The Suffusion theme also uses the wp_edit_nav_menu_walker hook, with a higher priority than my plugin. If you want to work with the Suffusion theme, I can suggest one work-around:

      De-activate the Suffusion theme, then use my plug-in for the menu. Next, enable the theme again. The settings from my plugin are saved and applied. The only thing that doesn’t seem to work is Appearances > Menus.

      In any case, the Suffusion theme should support the wp_edit_nav_menu_walker hook in order for this to work. Since it doesn’t, that is the reason you don’t see any differences.

      Kind regards,
      Diana

      [Edited to inlude new information]

      Reply
      1. Didier

        Hi Diana,

        Thanks for details and solution.
        I tried : “De-activate the Suffusion theme, then use my plug-in for the menu. Next, enable the theme again. The settings from my plugin are saved and applied. ”
        After re-activate Suffusion, it’s OK;
        But when I add something to Menu and save Menu with suffusion active : all comes back like before

        Kind Regards
        Didier

        Reply
      2. Didier

        Hi Diana,

        I ask Suffusion developer about this problem, but I had this answer in his forum (<snip>) :

        “Sorry, I have no idea what that means. I searched all my installed themes and I didn’t find wp_edit_nav_menu_walker anywhere (not even in TwentyTen or TwentyEleven). In fact, only Suffusion has wp_edit_nav_menu_walker, which it uses for Mega Menus. So I don’t know what the developer means when he says, “The Suffusion theme does not implement the wp_edit_nav_menu_walker hook”.”

        I’m just using wordpress… and I don’t understand where is the problem… but I think that lots of peopls would like to use your plugin.

        Could you give more details to Sayontan (Suffusion developer) ?

        Thanks

        Reply
        1. admin Post author

          I’ve updated my earlier reply, I made a mistake there: For a minute, I thought the Suffusion theme was overriding the Appearances > Menus page with a .php of their own. This is not the case, it seems to be that they are also using the wp_edit_nav_menu_walker hook, with a higher priority, which prevents my plug-in from using it. I will update the documentation accordingly.

          Reply
  13. Andre

    Hi,
    great plugin I think. Very useful. Thos little gems are making wordpress a great system. Thank You.
    πŸ™‚
    But, I am trying to figure out, why the dropdown tier only shows 6 not all posts that are in the category. I am already looking into the code.

    And what does the value “-1” really mean?
    I tried to enter 10 as number of posts there, but no changes at all.
    Thanks in advance.

    Cheers.

    Reply
    1. Andre

      Ah, one thing now is clear:
      In the plugin code it says: // default value of -1 returns all posts

      But it doesnt do so on my site to be frank.
      (I could only give You access over mail, if You would like to take a look, cause this is a clients’ site under development)

      Reply
      1. Andre

        err, I completely move back. The current WP version gives me the possibility to add articles to custom menus already.
        As I just want a link to the article and when all options in the “show options” dropdown (top right corner in custom menus admin page) are active I can already do a lot with them.
        Okay, so thank You anyway for this plugin, when it would be still needed, it was great. Thanks.

        Reply
        1. admin Post author

          Thanks for letting me know that you have chosen a different approach. If you ever encounter this problem again, I hope you will be able to provide some more information so that we can find the cause.

          Kind regards,
          Diana

          Reply
      2. admin Post author

        I am using my own plugin on this blog, you can see the resulton the right-hand side. The number of posts works as far as I can see, I’ve added eight posts with a custom taxonomy key and they are all showing. I’ve limited the number of posts under “Posts with category β€˜CPCM cat’” to 1, which also works.

        If it is not working for you, please help me improve my plugin by telling me what theme and which plugins you are using. If you could then revert to the default WordPress theme and disable all other plugins and see if the problem still occurs, together we can figure out what causes this.

        Kind regards,
        Diana

        Reply
  14. Vicky

    Hi there – we’d like to be able to use your plugin on our site, but to list links in the menu to our individual Portfolio posts.
    I’ve added the plugin and can easily bring up Portfolio Categories; but nothing displays on the public side in the drop down menu.
    Is this something we can tweak? Or outside the scope of your plugin.
    Thanks – your feedback is appreciated.

    Reply
    1. admin Post author

      The plug-in currently does not support custom post types. However, there’s a thread in the support forum suggestion how that functionality can be added. I will add it in a next version, but first I need to get around to testing it myself. In the meantime, check that out and apply the change to enable the plug-in for custom post types.

      Reply
  15. George

    Thanks alot for this very useful plugin!

    There’s a small bug though!
    In line 127:

    $post->title = str_replace(“%post_author”,$userdata ? $userdata->display_name : ”, $post->title);

    …should be:

    $post->title = str_replace( “%post_author”, $userdata ? $userdata->data->display_name : ”, $post->title);

    Reply
    1. admin Post author

      George, thank you for your feedback. I am at the moment unable to make any modifications to the plug-in (don’t have internet at home and can’t do this in the boss’s time), but as soon as I can I will fix this.

      Thank you for reporting!

      Reply
  16. Timothy Allen

    Hi, thanks for this plugin. One feature I was wanting was the ability to see if a menu item has children, so I can build drop-down responsive menus.

    The following diff adds this ability:
    — ../../plugins/category-posts-in-custom-menu/category-posts-in-custom-menu.php 2016-02-05 11:57:21.823900753 +0200
    +++ ../../plugins/category-posts-in-custom-menu/category-posts-in-custom-menu.php.b 2016-02-05 11:57:14.360002782 +0200
    @@ -378,6 +378,9 @@
    $query_arr = apply_filters( ‘cpcm_filter_posts_query’, $query_arr, $menu_item );
    }

    + // Notify that this menu item has children
    + $menu_item->classes = array_merge( $menu_item->classes, array(“menu-item-has-children”) );
    +
    $posts = get_posts( $query_arr );

    // Decide whether the original item needs to be preserved.

    … You may wish to move it below the remove_original_item code. Thanks!

    Reply
    1. admin Post author

      Hi TImothy,

      thank you for your suggestion. Because your request is not directly tied into my plugin (you might want this regardless of whether you are using my plugin) it’s best to do this in another way. I’d recommend using a standard WordPress filter named wp_nav_menu_objects and use that to modify the menu CSS classes after the menu has been generated.

      I’ve included a code sample in the sample CPCM extension at https://plugins.svn.wordpress.org/category-posts-in-custom-menu/trunk/sample-cpcm-extension/sample-cpcm-extension.php
      See the last block of code, consisting of a function definition and the call to add_filter to hook it up.

      Kind regards,
      Diana

      Reply
      1. Timothy Allen

        Thanks for the suggestion! You’re quite right, this seems like a better way of achieving the effect I wanted!

        Reply
  17. ehsan

    hi Diana,
    thanks for this plugin.
    The plugin is conflicts with wpml. my custom menu in default language is fine, but in the languages created by wpml does not work.

    Reply
    1. admin Post author

      Hi Ehsan,

      you’re right, it’s not compatible with WPML. I haven’t put in the effort to make it compatible. Anyone who is willing to do this can send the result to me and I’ll include it in the plugin.

      Kind regards,
      Diana

      Reply
  18. shane

    +1 one to custom clipping title length for menus. Im no WP guru so that would be a great feature, my website MCawarenessNZ.org has a lot of science journal articles with full length titles that are quite an eyefull. a simple syntax option for the menu item would be great, %post_title_limit20 or something like that…..

    Reply
    1. admin Post author

      Hi Shane,

      This can be done with an extension of my plugin. Although not extensively documented, the plugin comes with a sample extension included. Copy that to the plugin root directory and adapt it to replace your desired wildcard with the correct data.

      Kind regards,
      Diana

      Reply
  19. Peter

    awesome plugin ! has worked great on my site for a long time, but im changing themes and im not sure why but the the menu options that showed the “create submenu” “number of posts” etc doesnt show up when im using this new theme, not sure why,

    anyone have any idea how to fix this?

    going from divi to extra (elegantthemes) they are essentially the same theme though

    Reply
  20. Denise Melton

    Hi there

    Is there anyway to have the menu slideopen and show the submenu related only to the parent and then close when you click another parent link to open a different submenu? Hope that makes sense? Or do I have to add some jQuery to do this?

    Reply
    1. admin Post author

      Hi Denise,

      sorry for the terribly late reply. This must have got lost in my mailbox. This is not something related to my plugin, you need a theme developer for this. This would involve CSS or JavaScript (jQuery)

      Kind regards,
      Diana

      Reply
  21. Peretz

    Hi Diana – your plugin is very useful. I am wondering how I can expand the category queries to be and AND query so that a menu item can display posts that meet the conditions of having “Category A” AND “Category B” . (and avoid having to use Cat ID).
    Thank you!

    Reply
  22. wilbert

    At the moment the posts are created as submenu, this is very usefull!
    Only issu what i need is the main menu item, which is a link to display all posts from the category, needs to put off. It needs to display, but without the link to the posts of the specififc category.

    Main menu ( this link needs to be changed !!)
    sub menu post 1
    sub menu post 2

    How to?

    I am wondering if it is posisble to skip the link of the main menu item

    Reply
    1. admin Post author

      Hi Wilbert,

      sorry for the late reply.
      The only way to do this is to manually create a menu item that has as URL a hash #, thus not leading anywhere new.

      Kind regards,
      Diana

      Reply
  23. Mike

    Diana,

    I’ve got the plugin set up with all custom posts in a category (via a sub-menu) and it’s working beautifully.

    My setup is like this: Custom Link at top level, category (of custom posts) as the sub menu.

    Is it possible to go another level and do a sub-sub-menu?

    I tried putting the sub-sub category into the menu but it just added the sub-sub category items into the sub-category level. I also tried giving the sub-sub menu categories a parent of the sub category but that also put the sub-sub category items into the sub-category level.

    Does this make sense?

    Reply
    1. admin Post author

      Hi Mike,

      I’m not entirely sure, but what I think you can do is work with the “Remove original menu item: Always” setting.
      Try this:

      – Level 0: Add custom link (A).
      – Level 1: Below custom link (A), add another custom link B, titled “Category 1”
      – Level 2: Below link B, add your submenu for category 1 and choose “Remove… : always” setting. Exclude subcategories explicitly.
      – Level 2: Below link B, add another custom link (C), titled “Subcategory 1a”
      – Level 3: Below link C, add your sub-submenu for category 1a. Again, choose “Remove… : always”

      Does that do what you meant to do?
      Note: The plugin can indeed *not* do this automatically. You have to add subcategories manually one by one. (The plugin was never meant for extremely heavy use cases and there are some limitations in WordPress that make it impossible to build a full menu consisting of all subcategories for you.)

      Kind regards,
      Diana

      Reply
  24. Jirko

    Hey Diana,
    I would like to say “thank you” – your plugin still works great in wordpress version 4.8.
    Hopefully it will last a long time. Sometimes its hard to tell google what you are looking
    for and then you’re about searching your a** off.
    But finally i found your solution. – YEAY
    I will get you a coffee.

    Reply
    1. admin Post author

      Hi Jirko,

      thanks a lot for the message and the coffee!
      I’m glad to hear that my plugin is still useful after more than 10 years! πŸ˜€
      I really appreciate it. Have a great day!

      Kind regards,
      Diana

      Reply
  25. Dan

    Hi Diana
    I love your plugin, I am using it on my site http://www.attractionsnearme.co.uk and find it really helpful. I have found one minor issue using it with the Sydney theme in that when I make a change to one of my menus, I can save the changes but when I try to navigate to another area of my website I get the following message:
    Do you want to leave this site? Changes you made may not be saved. In fairness, it does state that the plugin has not been tested with my version of WordPress.
    I also use Geodirectory, it would be great if I could pick up the Geodirectory categories and create a submenu containing links to posts in this category as at the moment I have to add them individually using custom links.
    Regards Dan
    Attractions Near Me

    Reply
    1. admin Post author

      Hi Dan,

      Thanks for your message.

      The “Do you want to leave?” message is caused by GeoDirectory.
      I have added GeoDirectory to my test site (am not using Sydney theme) and I get this message as well. I would suggest you file a bug report with GeoDirectory.

      As for using GeoDirectory tags and categories: You can. I installed GeoDirectory, added categories and tags (Called ‘Places Tags’ and ‘Places Categories’) and then in Appearance > Menus, I can choose categories or tags from Places Tags and Places Categories. So, that works.

      Hope this helps.

      Kind regards,
      Diana

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *