• I am having an issue with the “Current States” and “Parent Pages” on a custom post type.

    Here is my setup:

    I have created a custom post type (Called Radiologist). Hierarchical is set to “true”. Capability Type is “post”.

    In the “Reading Settings”, my “Front Page” is set to be a static page called “Home”. My “Posts” (the blog) page is set to show on a static page called “News”.

    I have a third static Page called “Physicians”. When a visitor clicks on “Physicians”, they will see a list of all the radiologists (the radiologists being all the pages created with the new Custom Post Type).

    When a visitor clicks on a Radiologist’s name, they go that Custom Post Type.

    Here is the issue: WordPress seems to think that The Radiologist’s Parent Page is “News” (the blog page), when I want it to be “Physicians”. So the navigation is not showing “current_page_parent” on the wrong
    <li>.

Viewing 14 replies - 16 through 29 (of 29 total)
  • Here is the solution I am using for this problem. This is somewhat of a hack, but it’s getting the job done. You would place this code in your functions.php.

    function remove_parent($var)
    {
    	// check for current page values, return false if they exist.
    	if ($var == 'current_page_parent' || $var == 'current-menu-item' || $var == 'current-page-ancestor') { return false; }
    
    	return true;
    }
    
    function tg_add_class_to_menu($classes)
    {
    	// team is my custom post type
    	if (is_singular('team'))
    	{
    		// we're viewing a custom post type, so remove the 'current-page' from all menu items.
    		$classes = array_filter($classes, "remove_parent");
    
    		// add the current page class to a specific menu item.
    		if (in_array('menu-item-239', $classes)) $classes[] = 'current-page-ancestor';
    	}
    
    	return $classes;
    }
    
    if (!is_admin()) { add_filter('nav_menu_css_class', 'tg_add_class_to_menu'); }

    One downside: I’m using an ID to decide which menu item to add the current class to, so if you delete/readd a page you have to edit this code.

    @timstl thanks for the post. Maybe not a perfect fix, but exactly what I needed and works great.

    Thanks!

    Thanks timstl. With a few tweaks, that did the trick.

    PS is it just me or are the avatar positions on this forum borked?

    What were those tweaks you mentioned Tamlyn? I’m clearly missing something here…

    Why use !is_admin to filter the nav_menu_css_class?
    current-page-ancestor shoud be current_page_ancestor

    So this is what is working for me:

    https://wordpress.pastebin.com/T5VAPf1R

    Thank you timstl!

    As far as why to use !is_admin(), it’s really up to you. It’s probably not going to matter either way. I just knew I only wanted it to run on the front end, so I added it, but the code was written quickly w/o much research.

    I don’t know the circumstances under which current-page-ancestor vs current_page_ancestor are used by the menu system, but I have seen them both at times.

    I used a simple jquery script to handle my problem.

    $(function() {
    
    	if ($("body").hasClass("single-products")) {   // single-products is the name of my template page for the custom post type
    
    		$("li.page-item-19").removeClass("current_page_parent");   // Removes the class current_page_parent from the <li> with the page-id of the Blog (News)
    
    		$("li.page-item-11").addClass("current_page_parent");   // Adds the class current_page_parent to the <li> with the page-id of the Products page
    	}
    
    });

    Note: the body tag needs to have <?php body_class(); ?> for it to spit out the class for your custom post type page on the body tag.

    maseintheplace

    (@maseintheplace)

    blooomin eck! after a good 3 hours i finally got this working…

    thanks to ipixelsmediaworks!

    shame this is not simpler – ive been raving how cool wordpress is but this hack makes it feel dirty

    AndyiBM

    (@andyibm)

    Thanks timstl – works really well.

    Shame it’s necessary.

    mamoran

    (@mamoran)

    If anyone is still having this problem I have a fairly simple solution. I simply inserted a conditional in the header.php file when generating the nav elements.

    <?php if (is_singular('CPT-SINGULAR-NAME')) { ?>
         <ul id="projectNav">
              <?php wp_list_pages('title_li='); ?>
         </ul>
    <?php } else { ?>
         <ul id="nav">
              <?php wp_list_pages('title_li='); ?>
         </ul>
    <?php } ?>

    *Replace CPT-SINGULAR-NAME with your custom post type

    After that it was simply a matter of reformatting the css for the former “nav” element into the new “projectNav” element for the new custom post type.

    Hope this helps someone, and REALLY hope a more elegant solution is found for this problem soon.

    This is what I ended up doing. Since it checks based on the href and not the page ID, it prevents you from having to rewrite your code if you delete and recreate any pages.

    I made this for my “gallery” post type. Feel free to change it out for whatever post type you need. Mine checked for single gallery pages, gallery archive pages and archives for a custom taxonomy called “styles”.

    if ( $('body').hasClass('post-type-archive-gallery') || $('body').hasClass('tax-style') || $('body').hasClass('single-gallery') ) {
        var current_page = $('nav#primary-navigation ul li.current_page_parent');
        var gallery_page = $('nav#primary-navigation ul li a[href="/gallery"]');
        $(current_page).removeClass('current_page_parent');
        $(gallery_page).parent('li').addClass('current_page_parent');
      }

    When you create a custom menu item for the custom post type archive you have to include the entire url and not just ‘/your-custom-post-type-name’. If you use the entire url the wordpress url rewrite function will check it against all the other menu items as well as against all your pages and so on. What you end up with in the menu parent is something like ‘current-menu-ancestor current-menu-parent current_page_parent current_page_ancestor’.

    I have the same feature request. There should be a built in solution to assign a page as a parent and highlight its menu for a given custom post type.

    I have an index page and its menu item, both called “Portfolio”. I have a custom post type on its own template page called “Single-Projects”. Clearly I want “Single-Projects” to appear within “Portfolio” and highlight its menu. It is surprising that this is not built into the core WordPress when so many other features are. This is a legacy of WordPress starting out as a blog and not a CMS.

    For now I will try @mamoran and @david Calhoun’s code as I can follow the logic although it’s a patch. Thanks both of you for sharing your solutions.

    @snapsize – WP doesn’t do that. It will create a current-menu-item class if you click that particulat nav menu item, but beyond that, WP won’t link it.

    This answer on stack exchange is a very good solution. I had to change $post_type = get_query_var('post_type'); to $post_type = get_post_type(); for the filter to function properly, but seems to work perfectly with that done. Obv change current-post-type to current-ancestor or whatever, I was just using it to keep items highlighted and it helped that it was that class. Won’t help with having a static page as an index, but you don’t really need one with post type archives available.

Viewing 14 replies - 16 through 29 (of 29 total)
  • The topic ‘Custom Post Type Parent’ is closed to new replies.