Menu item only on one category page and posts in that category in WordPress

This is one of those ‘blogging to remember’ posts. I’m working on using WordPress as a CMS on a site that has 700+ pages. In order to properly organize things, I need to be able to mix ‘news’ (aka the blog part) with the ‘static’ pages (aka the pages). In other words: I need a side menu (aka widget) that shows links to relevant pages on some of my posts.

In trying to figure out a solution, the biggest problem was that you have to remember that getting things done on WordPress PAGES is very different from getting the same thing done on WordPress POSTS, though I think the below method could be adapted to work for both. It’s specifically made however to get links to PAGES showing up on specific POSTS. For the reverse other methods are probably more efficient.

This turns out to be doable, though not easy, using a combination of the Query Posts plugin and the Widget Logic plugin.

So here’s the plan:

  1. Show links to the children of one page, using Query Posts
  2. Show a widget only on posts in a category, and the category page, using Widget Logic

Show links to the children of one page, using Query Posts

This is the hard part: finding out what the post ID is of the page whose children you want to show. On a site that uses WordPress as a CMS, you’re likely to use Pagemash to organize your pages. An added benefit is that it has information on each page included, like the page ID.

Note that number down somewhere. Now we’re on to the Query Posts widget. It’s a bit scary, but not as scary as the PHP you’d have to type to get the same result, so we’ll dive right in.

  1. Install Query Posts.
  2. Head over to ‘widgets’.
  3. Drag the Query Posts Widget to the place in your sidebar where you want your menu item to show.
  4. Open it up. Your settings are going to be:
    1. Title: what you want to show at the top of the widget (easy, right)
    2. Display: ul – assuming you want the widget to look like every other widget you have.
    3. Post_status: leave blank or set to ‘published’
    4. Post_type: Select PAGE
    5. The next fields are left empty, till you come to:
    6. Post_parent: enter the number you found using pagemash: the number of the page you need to see the children of.
    7. You don’t need any other fields, so just leave them empty.
    8. Click ‘save’ for the settings of the widget to be saved
  5. Head over to your blog to see the result. An extra menu featuring the children of that page you selected should appear everywhere on your site. We’ll get to how to limit that next.

Show your widget only on some posts, using Widget Logic

Install and activate the Widget Logic plugin. Then post the following code into the newly appeared field that appears at the bottom of your query posts widget (and every other widget).

is_category(‘X’) || (is_single() && in_category(‘X’))

Where X is the category-slug. The category-slug is the bit of text you see at the end of the URL of that category page. If you’re not sure, you can find the category slug under ‘categories’, in the ‘posts’ menu. Here’s what the rest of the code means:

is_category(‘X’) is for showing the widget on the category page.

|| is for OR

(is_single() && in_category(‘X’)) is for single pages in category X.

That should be it. Click ‘save’ again in the widget, and head over to your site to check whether the extra menu item REALLY only shows up on the posts in that category, and on that category page. If it shows up nowhere, you mistyped your PHP somewhere. The field in the Widget Logic plugin is a bit small, so open a text editor to check it out. Do you have apostrophes where you need to? Are all your brackets closed (that is: does every opening bracket ‘(‘ have a corresponding closing bracket ‘)’)?

Good luck.

Added fun: showing the parent page as well!

Unfortunately the above method does not allow for showing the parent-page as well as the children of that parent, so what I ended up doing was create a dummy page that redirects to the parent page. It shows up in the menu, but when people click on it, they get to the parent page. This sort of redirection stuff is easily managed wit the Quick Page/Post Redirect Plugin. It’s such a simple redirect plugin, anybody can use it. You know, no messing with .htaccess and any of that. It just ads an extra field to each page and post and tells you to enter the URL you want it to redirect to. For an post or page within your CMS, you can simply use the page slug.

If you’re going to use this for permanent redirects like in my example, select ‘301 redirect’. If it’s temporary, use ‘302 redirect’.