Category Posts in Custom Menu


Download

You can get the plugin here. And you can see a sample menu at the right hand side, including a link to a post that shows how this sample menu was created.


Contributors: anaid

Tags: menu, category, post tag, posts, dynamic, automatic

Requires at least: 3.2.1

Tested up to: 3.4.2

Stable tag: 0.7.1

Dynamic menus: This plugin automatically replaces selected Category links / Post Tag links / Custom Taxonomy links in a Custom Menu by a list of their posts/pages.

Description

The custom menu only allows adding a link that leads to a list of posts and pages belonging to a taxonomy (category, tag or custom). I wanted to insert the posts and pages themselves into the menu, so I wrote this plug-in. If you want to list the posts and pages belonging to a certain taxonomy in the menu and if you want WordPress to do this automatically for you, then I hope you will find this plugin useful.

Now also works for custom post taxonomies and custom post types!

Using it

Enable the plugin and go to Appearance > Menus. Create your Custom Menu. Add a taxonomy (category/tag/custom) to the menu. Next, you can choose if you would like to replace the original link (to a page that lists the items) by the posts and pages themselves.

For each Category or Post Tag item in the menu, you now have the following extra configuration options:

  • replace the link by a list of posts in the menu y/n;
  • specify the (maximum) number of posts you want to show for this item (-1 = all);
  • their ordering (ascending/descending by none, ID, author, title, date, modified, parent, rand or comment_count);
  • specify the title for the menu items. For this you can use %post_title, %post_author and other wildcards, see ‘Other Notes > Wildcards’.

Details

This plugin uses the wp_nav_menu_objects filter hook to remove and replace category items in the menu by the posts in that category. It extends Walker_Nav_Menu_Edit to add input fields to Appearance > Menus and adds some CSS to style this properly.

Other Notes

Wildcards

You can control the title of the menu items generated by the plug-in with the following wildcards:

  • %post_title
  • %post_author
  • %post_feat_image (url only)
  • %post_excerpt
  • %post_url
  • %post_date for the unformatted post date: 2012-11-10 18:14:23
  • %post_date() for a formatted post date with the following default formatting: November 10th, 2012
  • %post_date(<your formatting here>) for custom date formatting. Example: %post_date(l jS \of F Y, h:i:s A). See http://codex.wordpress.org/Formatting_Date_and_Time for formatting options.
  • %post_date_gmt
  • %post_date_gmt()
  • %post_date_gmt(<your formatting here>)
  • %post_status
  • %post_modified
  • %post_modified()
  • %post_modified(<your formatting here>)
  • %post_modified_gmt
  • %post_modified_gmt()
  • %post_modified_gmt(<your formatting here>)
  • %post_comment_count
  • wildcards for custom fields, like %post_my_field, where ‘my field’ or ‘my_field’ is a custom field

Tips and tricks

Partial links

then WordPress creates, for example:

<a href=”link_to_post1″>Chocolate pie by Anna</a>
which results in
Chocolate pie by Anna

Some things you can do:

  • Set title: %post_title</a> by %post_author

    Then WordPress creates:

    <a href=”link_to_post1″>Chocolate pie</a> by Anna</a>
    which results in
    Chocolate pie by Anna

    (Yes, there’s an extra closing </a> but that doesn’t really matter.)

  • Set title: </a><a href=”http://www.mysite.com/something/”>%post_title</a> by %post_author

    Then WordPress creates:
    <a href=”link_to_post1″></a><a href=”http://www.mysite.com/something/”>Chocolate pie</a> by Anna</a>
    <a href=”link_to_post2″></a><a href=”http://www.mysite.com/something/”>Strawberry muffins</a> by Susan</a>

    which results in
    Chocolate pie by Anna
    Strawberry muffins by Susan

    Note that all post links will point to the same URL.

  • Set title: </a><a href=”%post_url#myhashtag”>%post_title</a> by %post_author

    Then WordPress creates:
    <a href=”link_to_post1″></a><a href=”link_to_post1#myhashtag”>Chocolate pie</a> by Anna</a>
    <a href=”link_to_post2″></a><a href=”link_to_post2#myhashtag”>Strawberry muffins</a> by Susan</a>

    which results in
    Chocolate pie by Anna
    Strawberry muffins by Susan

    You can use this to have menu item links skip to a certain paragraph if you’re using anchors in your HTML.

You can freely use HTML in the menu items.

Changelog

0.7.1

  • Bugfix: Link Target, CSS Classes, Link Relationship (XFN) and Description weren’t inherited from the original menu item. They are now.

0.7

  • Added support for custom post types
  • Fixed user_data bug
  • Added date formatting options

0.6

  • Added a bunch of wildcards: %post_feat_image (url only), %post_excerpt, %post_url, %post_date, %post_date_gmt, %post_status, %post_modified, %post_modified_gmt, %post_comment_count

0.5

  • Enable custom taxonomies.
  • Change plugin URL.

0.4

  • Fix plugin bug for Tags, probably related to 3.2.2 update.
  • Edit documentation
  • Change some CSS to show checkbox next to label instead of above it.
  • Edit text in menu item block to clarify use of custom fields.

0.3

  • Extended the plugin for use of custom fields. Use a custom field with name ‘my field’ with wildcard %post_my_field. Note that if the post does not have this custom field, the wildcard is removed (it is not replaced by anything).

0.2

  • Extended the plugin to post tags. Note that the plugin name has unfortunately _not_ been changed to reflect this extension.

Added checkboxes, dropdowns and text fields to Appearance > Menu. It is now possible to:

  • Enable/disable per taxonomy item whether you want to replace it by its posts
  • Specify the (maximum) number of posts that should be displayed.
  • Specify the ordering (Descending/Ascending) and the field to order by (ID, title, etc..)
  • Set a title for the menu items. For this you can use the wildcards %post_title and %post_author.

Installation

1. Upload `category-posts-in-custom-menu.php` to the `/wp-content/plugins/` directory

2. Activate the plugin through the ‘Plugins’ menu in WordPress

Frequently Asked Questions

Can’t see it working in any way

Problem: “I can’t see what effect it has had. Nothing has changed (not that I can see) in the Appearance > Menu page. Just like before.”

Two possible causes:

  • Issue 14527 (http://core.trac.wordpress.org/ticket/14527) is the cause. When adding a menu item but before saving it, you will indeed see none of the promised checkboxes. So be sure to save your menu after you add a Category/Tag to your menu. Then you’ll see the added functionality.

    The reason is because the hook that is used to extend the functionality is not being applied until you’ve saved the item. A patch was approved for WordPress 3.4, so upgrade your WordPress installation. :)

  • You’re already using another plugin that uses the wp_nav_menu_edit hook and has a higher priority than mine. This is by design: My plugin is not critical and therefore I don’t claim a high priority on the hooks. Plugins/Themes that indicate to be more important will be given priority.
  • You’re using a theme that somehow doesn’t support editing the menu. Try using the default WordPress theme to see if your problem persists. I can’t offer much support if the theme is the problem, because this is my first and only plugin and I wouldn’t know where to begin to solve theme problems..

Screenshots

Go to Appearance > Menu to enable/disable and set the options for the replacement of Category / Post Tag links by their posts.