WordPress.org

Ready to get started?Download WordPress

Forums

Why does Blog become current_page_parent with Custom Post Type? (8 posts)

  1. Piet
    Member
    Posted 3 years ago #

    I have a problem with my single Custom Post Type and the navigation menu.

    My Custom Post Type is called "project", it's non-hierarchical and I have disabled the archive. Instead I am using a Page Template called projects.php. I have also made a template single-project.php to show each separate project on its own page.

    Now the problem is that when I browse to a single project, in the nav-menu all of a sudden my Blog (News) gets the class current_page_item.

    1. How is this possible?
    2. How do I get rid of it?
    3. (optional) Is it possible to give my Projects Page this class

    Thanks in advance for any help and/or tips!

  2. brandwall
    Member
    Posted 3 years ago #

    Piet, I am having the same problem, I've read a lot about fixes but I haven't quite figured it out. Could someone please give us some insight? Any help will be greatly appreciated! Thanks!

  3. Piet
    Member
    Posted 3 years ago #

    Hi Brandwall, as you can see from the amount of comments on this thread I haven't really gotten any feedback on this.

    I found that the only possible way to do it, is to enable the archive.

    Then I completely redid the CSS for my nav-menu and eventually I got it to work.

    Sorry that I can't give you anymore useful advice, the problem lies mostly in the CSS.

  4. brandwall
    Member
    Posted 3 years ago #

    ok Thanks so much for responding. I think I just found a fix for it by putting this code into the functions.php and changing afew items. For my case I have two custom post types 'motion' 'still'.

    here is some code I found...

    // 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 'motion':
         		// 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-592', $classes))
         		{
         		   $classes[] = 'current_page_parent';
             }
         		break;
    
         	case 'still':
         		// 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-348', $classes))
         		{
         		   $classes[] = 'current_page_parent';
                   }
         		break;
    
          // add more cases if necessary and/or a default
         }
    	return $classes;
    }
    add_filter('nav_menu_css_class', 'add_class_to_wp_nav_menu');
  5. Piet
    Member
    Posted 3 years ago #

    That looks great brandwall, thanks so much!

  6. ElShaddai Edwards
    Member
    Posted 2 years ago #

    Very helpful - solved the problem. Thanks!

  7. Dreilwood
    Member
    Posted 2 years ago #

    Thank you, thank you, thank you :D. Worked a treat!

  8. eratosthene
    Member
    Posted 2 years ago #

    Thanks a lot... I have had this problem for 5 days and you solved it in few seconds.
    See you.

Topic Closed

This topic has been closed to new replies.

About this Topic