Skip to content

Instantly share code, notes, and snippets.

@jjeaton
Created May 5, 2013 20:06
Show Gist options
  • Save jjeaton/5522014 to your computer and use it in GitHub Desktop.
Save jjeaton/5522014 to your computer and use it in GitHub Desktop.
Remove current_page_parent class from Blog menu item when using custom post types and add class for the post type menu item. Hardcodes the menu-item title for each.
<?php
add_filter( 'nav_menu_css_class', 'je_portfolio_menu_item_classes', 10, 2 );
/**
* Add css classes to Portfolio CPT menu item, remove from Blog item
*
* Enables menu classes for CPTs.
* Pretty fragile, as it depends on the item titles for each menu item, change as required
*
* @param array $classes CSS classes for the menu item
* @param object $item WP_Post object for current menu item
*
* @link http://www.josheaton.org/
* @author Josh Eaton <josh@josheaton.org>
* @copyright Copyright (c) 2013, Josh Eaton
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/
function je_portfolio_menu_item_classes( $classes, $item )
{
switch ( get_query_var('post_type') ) {
// Only run on portfolio post type
case 'portfolio':
// Remove current_page_parent from Blog menu item
if( $item->title == 'Blog' ) {
$classes = array_diff( $classes, array( 'current_page_parent' ) );
}
// Add current_page_parent class to the portfolio menu item
if ( $item->title == 'Portfolio' ) {
$classes[] = 'current_page_parent';
}
break;
}
return $classes;
}
@isarmstrong
Copy link

Wonderfully simple. This could easily be made dynamic with an options panel and a couple of foreach loops too.

@jennybeaumont
Copy link

Brilliant, thank you!

@Wizardinfosys
Copy link

Thanks you

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