Skip to content

Instantly share code, notes, and snippets.

@jeremykendall
Last active December 26, 2015 18:08
Show Gist options
  • Save jeremykendall/7191714 to your computer and use it in GitHub Desktop.
Save jeremykendall/7191714 to your computer and use it in GitHub Desktop.
To have to resort to a hack like this to get nav highlighting for a custom post type is ridiculous. Especially when it's a feature that's been requested for 3+ years (http://core.trac.wordpress.org/ticket/16382) and Google is chock full of "How the f*ck do I do this?" results: https://www.google.com/search?q=wp+current_page_parent+cpt&oq=wp+curr…
<?php
add_filter('nav_menu_css_class', 'namespace_menu_classes', 10, 2);
function namespace_menu_classes($classes , $item)
{
if (get_post_type() == 'company' || is_archive('company')) {
$classes = str_replace('current_page_parent', '', $classes);
if (strpos($item->url, 'portfolio') !== false) {
array_push($classes, 'current-menu-parent');
}
}
return $classes;
}
@nacin
Copy link

nacin commented Oct 28, 2013

capability_type is purely for user/role capabilities — as in, current_user_can(). It specifically says "Yes, use edit_posts and similar," or "Use edit_pages and similar." Or, it can be paired with the capabilities and map_meta_cap flags for more power.

This isn't "a feature that's been requested for 3+ years". It was requested once 3 years ago, and the person who wrote the nav menu API said "nope, it works this other way". After there was no response, I closed out the ticket.

It is important for maintainers to change their minds when presented with good rationale, or generally just better information. If we were mistaken three years ago, then say so! My tweet wasn't a "RTFM", it was an honest "but there's a workaround, it doesn't work?" question. There's no need for passive-aggressive or angry tweets, gists, comments, and the like. We're not trying to make your life more difficult. But if you do, I'm really just not inclined to help at all. Help us help you.

@al-the-x
Copy link

@nacin This is probably just a case of @jeremykendall coming off as less of a nice guy than he actually is... Which is why I don't advocate (angry|tired|drunk) (tweet|cod|driv)ing.

FWIW, the approach you took is about as close to correct as you get. Using a filter hook to remove the unwanted class and inject the desired one isn't beginner Wordpress, so you nailed it. Based on your comments above, the hierarchical is really not what you're looking for unless your type is supposed to have explicit parents, like pages and sub-pages.

To your point, though, and regarding our Twitter discussion, there's definite room for improvement in the Codex and arguably in the convention employed by the Walker class. The decisions to lump custom post types under posts was a left-over from the early days of custom post types, IIRC. In WP, everything that isn't a page is really a post, and pages are just highly specialized posts at that. There really should be a way to tell a NavItem that it's a parent for a custom post type, but I'm not sure where.

For my own personal reference (when I come back to this in a couple of weeks for Word Camp ORL), try out the effect of show_in_nav_menus in the post type definition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment