Breadcrumb for hierarchical custom post type

#522379
  • Resolved Khwarizma
    Rank Math free

    Hi,

    I have created a hierarchical custom post type, books and it is associated with a taxonomy – book chapters.

    For example:
    Book 1 is a child page of Book
    – A post is a child of Book 1 and belongs to category Section 1.1 which is a subcategory Chapter 1.

    I’m wondering if it is possible to modify the breadcrumb so it shows both parent page and categories

    Home>Parent page>Child page>Category>Sub-Category>Post title

    So, it will like

    Home > Books > Book 1 > Chapter 1 > Section 1.1 > Post title

    I have tried changing the settings of the breadcrumbs, using RankMath, and this is what I get:

    Home > Books > Book 1 > Post title

    The category and subcategory is missing.

    Thank you for your help.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hello,

    Thank you for contacting Rank Math and bringing your concern to our attention. We deeply apologize for the delay in response.

    Please try applying the following filter to see if that works for you:

    add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
    	$post_type = get_post_type(get_queried_object());
    	
    	if ( ($post_type == 'your_post_type') ) { //replace 'your_post_type' with your CPT slug
    		$category = get_the_category();
    		$Category = [$category[1]->name, get_category_link( $category[1]->cat_ID ) ];
    		$subCategory = [$category[0]->name, get_category_link( $category[0]->cat_ID ) ];
    		array_splice( $crumbs, 3, 0, array($Category, $subCategory) );
    	} 
    	return $crumbs;
    }, 10, 2);

    Here’s how you can add filter/hook to your WordPress site:
    https://rankmath.com/kb/wordpress-hooks-actions-filters/

    Let us know how it goes. Looking forward to helping you.

    Thank you.

    Thank you very much for your help. I really appreciate it.

    Because I have a custom taxonomy, I use get_the_terms instead of get_category.

    if ( ($post_type == 'my_cpt') ) { //replace 'your_post_type' with your CPT slug
    		$crumbs[1][0] = 'my_custom_name';
    		$category = get_the_terms( $post->ID, 'my_custom_taxonomy' );
    		$Category = [$category[1]->name, get_term_link( $category[1]->term_ID ) ];
    		$subCategory = [$category[0]->name, get_term_link( $category[0]->term_ID ) ];
    		array_splice( $crumbs, 4, 0, array($Category, $subCategory) );
    	} 

    The result is as what I’d like to have:

    Home>Parent page>Child page>Category>Sub-Category>Post title

    However, when I click Category and Sub-category, I am redirected to the post and not to the archive of the category. I’m still trying to figure out how to get the link of the parent category. My apology, I’m still new to PHP.

    Another problem that I still have is that when I click the Child page, the breadcrumb becomes

    Home>Parent page>Child page> > >

    Still showing >.

    What should I do.

    Thank you.

    Hello,

    We deeply apologize for the delay in response.

    We might need to take a closer look at the settings. Please edit the first post on this ticket and include your WordPress & FTP logins alongside with a few example post URLs in the designated Sensitive Data section.

    Please do take a complete backup of your website before sharing the information with us.
    Sensitive Data Section

    It is completely secure and only our support staff has access to that section. If you want, you can use the below plugin to generate a temporary login URL to your website and share that with us instead:

    https://wordpress.org/plugins/temporary-login-without-password/

    You can use the above plugin in conjunction with the WP Security Audit Log to monitor what changes our staff might make on your website (if any):

    https://wordpress.org/plugins/wp-security-audit-log/

    Looking forward to helping you.

    Thank you.

    Hello,

    I have updated the sensitive data as requested. Can you please check further?

    Thank you.

    Nigel
    Rank Math business

    Hello,

    I changed term_ID to term_id in your code snippet. and the category URLs are displaying correctly now.

    Hope that helps. Please let us know if you have questions.

    Yes. That works
    Thank you

    Hello,

    We are super happy that this resolved your issue. If you have any other questions in the future, know that we are here to help you.

    If you don’t mind me asking, could you please leave us a review (if you haven’t already) on https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post about your overall experience with Rank Math? We appreciate your time and patience.

    If you do have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.

    Thank you.

Viewing 7 replies - 1 through 7 (of 7 total)

The ticket ‘Breadcrumb for hierarchical custom post type’ is closed to new replies.