WordPress.org

Ready to get started?Download WordPress

Forums

Custom Post Type Parent (30 posts)

  1. pikemurdy
    Member
    Posted 4 years ago #

    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>.

  2. pasiphilo
    Member
    Posted 4 years ago #

    I'm having the same issue. In my scenario, I have a "Speakers" page (just a regular "Page" post type) using a custom template that lists out all items of my custom "Speaker" post type. Clicking on a "Speaker" in this list brings you to that individual "Speaker" post. This is all working, however, my custom post type "Speaker" seems to automatically be made a child of my blog page (in this case, entitled "News") instead of my "Speakers" page.

    Is there a way to force a specific page to be the "parent" of a specific post type?

    pikemurdy, one possible work-around for you could be to manually add those Radiologists as children of your Physicians page in a custom menu in the Appearance > Menus screen. Doing this does seem to properly mark the parent of the post type based on its position in the custom navigation. This isn't ideal, however, because, at least in my case, I don't want to have to manually add each "Speaker" to a custom navigation.

  3. cippoo
    Member
    Posted 4 years ago #

    @pasiphilo I did that but it added one more page_parent to the blog and on to my custom post. Weird

  4. lorpag
    Member
    Posted 4 years ago #

    I have the same issue !!
    Nobody has a solution?

    My Scenario:
    I have "product" as custom_post_type,
    a Page called "products" that use a custom template.
    When i go to the single-product page my menu add a css class "current_page_parent" to the blog page but i would have the same class only on "products page" list element.

  5. petebarr
    Member
    Posted 4 years ago #

    I have the same issue and I'm pulling my hair out!

    Can anybody help?

  6. mindthemedia
    Member
    Posted 3 years ago #

    Me Too, same problem... Help! Anybody?

  7. fcalloni
    Member
    Posted 3 years ago #

    I too have the same problem and couldn't find anything to help.

    As a dirty, temporary solution (hope someone finds one!) I've used javascript and jQuery to change the selected menu item, hope it helps ;)

    <!-- Dirty hack to change current menu item (current_page_parent) -->
    <script type="text/javascript" language="javascript">
    $(document).ready( function (){
    
    	// change this with the menu ID which SHOULDN'T be selected.
    	var temp = $('#menu-item-34').attr('class');
    
    	// removes 'current_page_parent' class from current selected menu item.
    	temp = temp.replace("current_page_parent ", "");
    	$('#menu-item-34').attr('class', temp);
    
    	// change this with the menu ID which SHOULD be selected.
    	temp = $('#menu-item-43').attr('class');
    
    	// adds 'current_page_parent' class to desired menu item.
    	temp = "current_page_parent " + temp;
    	$('#menu-item-43').attr('class',temp);
    
    });
    </script>
  8. Scope
    Member
    Posted 3 years ago #

    ive run into the same issue, really is odd... doesnt this defeat the purpose entirely of touting it as a "cms like" capability?

    I started by creating a custom post type called "Team"...and a regular page called "our team" wich acts as an index page... just a page template pulling in the custom "Team" pages.

    in my case my custom post type is set to "page"... i set the 'supports' argument when registering the custom post type to include 'page-attributes' thinking this would let me bring in the default parent drop down... wich would open up the existing 'pages' to set one of them as the parent.

    With the idea being that i could select the static "our team" page to be the parent. thereby integrating it all menu-wise.

    but even though the page attributes box does appear in the custom write panel, the 'Parent' drop down menu only shows pages of this particular custom type (team pages) wich is unfortunate, and doesnt make sense to me as these pages are essentially siblings.

    i had assumed this is what would have let us set ANY existing page as 'parent'... as it is, there doesnt seem to be a way to have a proper "top level" page for custom pages.

    (on another note, the page template dropdown is missing entirely as well, wich was another surprise. it seems that the despite the term 'capability_type' A custom "page" type cannot use page template?

    these custom post types dont quite actually do what posts/pages are capable of.

    *******************

    I was thinking of creating an "our team" top level page as a custom post type and setting it as the parent of the other team pages (since now it would appear as an option) and then running a conditional statement in the custom single post template... and adding in the appropriate code that way (single post code if its a child, get all posts code if its a parent)

    But that would cause the same issue cippo mentioned above... double "parent" labels in the url. it could probably still work out with creative labeling, but it still means custom post types would always be 3 levels deep.

    ....good god i rambled

  9. Naoise Golden Santos
    Member
    Posted 3 years ago #

    same problem here!

    trying to create a single-custom-post-type-name.php but the menu item with current_page_item class is the Blog page item instead of the custom page with "Custom Post Type Name" Template!

    Having huge problems creating child themes with Thematic and Custom Post Type... it makes me want to work with pages instead of custom post types

  10. beetrootman
    Member
    Posted 3 years ago #

    This annoying me as well. It basically renders the whole current_parent_item class naming pointless, since if you use posts the only parent you ever have is blog.

    The only was I can think of to get around it is creating a series of child pages and custom page templates like so:

    Parent:Work Page: Shows all custom type work posts.
    child:Single work page: Shows single work post.
    child:Single work cat page: If you have some categories for work posts.
    chuld:Single work archive page: Archive of work posts.

    I think, in theory, the current_page_item WOULD be 'Work page' for all the child pages, even though they are showings posts. The annoying this about this is that you have to make loads of pages and different templates. I'll test it out and let you know if it works.

  11. beetrootman
    Member
    Posted 3 years ago #

    No wait, that wouldn't work because when you view a single post it always uses single.php or a variation of it. No way of displaying single posts through a page template as far as I'm aware.

    There must be some way of hacking this.

  12. syrus69
    Member
    Posted 3 years ago #

    i am having a similar problem with my CPT aswell,
    but my issue is that how do i separate the normal blog posts and CPT
    i have hunted the net for solutions but can not find any

  13. beetrootman
    Member
    Posted 3 years ago #

    different issue entirely. I will PM you some info.

  14. fcalloni
    Member
    Posted 3 years ago #

    i am having a similar problem with my CPT aswell,
    but my issue is that how do i separate the normal blog posts and CPT
    i have hunted the net for solutions but can not find any

    hi syrus69, to show only your custom posts create a page (i.e. page-artwork.php my case) and set your own WP_Query - have a code like this on the page:

    <?php
    $type = 'sf1_artist';
    $args=array(
      'post_type' => $type,
      'post_status' => 'publish',
      'paged' => $paged,
      'posts_per_page' => 4,
      'caller_get_posts'=> 1
    );
    $temp = $wp_query;  // assign orginal query to temp variable for later use
    $wp_query = null;
    $wp_query = new WP_Query($args); 
    
    get_template_part( 'loop', 'artists' );
    ?>

    for more info check these:
    http://codex.wordpress.org/Function_Reference/WP_Query
    http://codex.wordpress.org/The_Loop

  15. syrus69
    Member
    Posted 3 years ago #

    cheers mate will look into it

  16. miradev
    Member
    Posted 3 years ago #

    I think this is a fairly generic and common requirement that CPT has really highlighted. The concept of post-type-hierarchy would solve most of these problems.

    In the classic 'music collection' example, you would have a CPT of 'Album' that had child CPT of 'Track'. Adding new 'Track' posts would require the assignation of a parent 'Album' post.

  17. Tim
    Member
    Posted 3 years ago #

    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.

  18. Patrick
    Member
    Posted 3 years ago #

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

    Thanks!

  19. tamlyn
    Member
    Posted 3 years ago #

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

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

  20. Jason
    Member
    Posted 3 years ago #

    What were those tweaks you mentioned Tamlyn? I'm clearly missing something hereā€¦

  21. cannobbio
    Member
    Posted 3 years ago #

    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:

    http://wordpress.pastebin.com/T5VAPf1R

    Thank you timstl!

  22. Tim
    Member
    Posted 3 years ago #

    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.

  23. ipixelsmediaworks
    Member
    Posted 3 years ago #

    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.

  24. maseintheplace
    Member
    Posted 3 years ago #

    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

  25. AndyiBM
    Member
    Posted 3 years ago #

    Thanks timstl - works really well.

    Shame it's necessary.

  26. mamoran
    Member
    Posted 3 years ago #

    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.

  27. David Calhoun
    Member
    Posted 3 years ago #

    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');
      }
  28. Denis Leblanc
    Member
    Posted 3 years ago #

    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'.

  29. edwinbradford
    Member
    Posted 3 years ago #

    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.

  30. Dan Couper
    Member
    Posted 3 years ago #

    @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.

Topic Closed

This topic has been closed to new replies.

About this Topic