Created
May 5, 2013 20:06
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 <[email protected]> | |
* @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; | |
} |
Brilliant, thank you!
Thanks you
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wonderfully simple. This could easily be made dynamic with an options panel and a couple of foreach loops too.