Skip to content

Instantly share code, notes, and snippets.

@yratof
Created September 17, 2015 10:34
Show Gist options
  • Save yratof/4181a921066e02ef4692 to your computer and use it in GitHub Desktop.
Save yratof/4181a921066e02ef4692 to your computer and use it in GitHub Desktop.
Change CTP single's parent from Blog to another page
<?php
// As of WP 3.1.1 addition of classes for css styling to parents of custom post types doesn't exist.
// We want the correct classes added to the correct custom post type parent in the wp-nav-menu for css styling and highlighting, so we're modifying each individually...
// The id of each link is required for each one you want to modify
// Place this in your WordPress functions.php file
function remove_parent_classes($class) {
// check for current page classes, return false if they exist.
return ($class == 'current_page_item' || $class == 'current_page_parent' || $class == 'current_page_ancestor' || $class == 'current-menu-item') ? FALSE : TRUE;
}
function add_class_to_wp_nav_menu($classes) {
switch (get_post_type()) {
case 'project': // CHANGE THIS TO YOUR CPT SLUG
// we're viewing a custom post type, so remove the 'current_page_xxx and current-menu-item' from all menu items.
$classes = array_filter($classes, "remove_parent_classes");
// add the current page class to a specific menu item (replace ###).
if (in_array('menu-item-21', $classes))
{
$classes[] = 'current_page_parent';
}
break;
}
return $classes;
}
add_filter('nav_menu_css_class', 'add_class_to_wp_nav_menu');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment