Breadcrumb on ACF Posttypes

#782441
  • Resolved Anonymous
    Rank Math free

    Hi team,

    We would like to add the Posttype in our Breadcrumbs. These Posttypes for event, jobs, blog, news, person, are created via ACF.
    Is there any chance to add them? Or could you provide a code for that?

    Thanks in advance for your help!

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

    Thank you for reaching out to us regarding adding Posttypes created via ACF to your breadcrumbs. To include custom Posttypes like events, jobs, blog, news, person, created via ACF in your breadcrumbs, you can use the following code snippet:

    /**
     * Filter to add custom Posttypes to the breadcrumbs
     *
     * @param array       $crumbs The crumbs array.
     * @param Breadcrumbs $this   Current breadcrumb object.
     */
    add_filter(
        'rank_math/frontend/breadcrumb/items',
        function ( $crumbs, $class ) {
            // Check if we are viewing a custom Posttype.
            if ( is_singular( array( 'event', 'jobs', 'blog', 'news', 'person' ) ) ) {
                $post_type = get_post_type_object( get_post_type() );
                $crumbs[] = array( $post_type->labels->name, get_post_type_archive_link( $post_type->name ) );
            }
            // And return the breadcrumbs.
            return $crumbs;
        },
        10,
        2
    );

    You can add this code snippet to your theme’s functions.php file or use a custom plugin like Code Snippets to add this functionality.

    If you encounter any issues or need further assistance, please feel free to reach out to us. We are here 24/7 to help you.

    Thank you for using Rank Math!

    Sources:
    How to Customize Breadcrumbs in Rank Math » Rank Math
    How to Enable Breadcrumbs on Your Website With Rank Math SEO » Rank Math
    How to Fix “Either ‘name’ or ‘item.name’ should be specified” Error » Rank Math
    How to Integrate Rank Math With Advanced Custom Fields » Rank Math

    Anonymous
    Rank Math free

    how canI set this up to be on second place, e.g. Home | Blog | Title of Blog

    Anonymous
    Rank Math free

    how can I change the order to Home | Posttype | Title of Posttype in the code above?

    Hello,

    Please follow the filter code below to get the desired breadcrumbs item:

    add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
    	$post_type = get_post_type(get_queried_object());
    	
    	if ($post_type == 'publication') {
    		$crumbs[1][0] = 'Publications';
    		$crumbs[1][1] = 'https://EXAMPLE.COM/publication/'; // URL of CPT slug
    		$crumbs[2][0] = get_the_title();
    	}
    
    	return $crumbs;
    }, 10, 2);

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

    Anonymous
    Rank Math free

    so we have to put in this code for each posttype we have?
    We have Blog, News, Jobs, Publications, Events, …

    Let me know if this is what we have to do.
    Thanks!

    Hello,

    You will have to modify the code and add multiple conditions to check for the post type and add it to the breadcrumbs. Here is an example:

    add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
    	$post_type = get_post_type(get_queried_object());
    	
    	if ($post_type == 'publication') {
    		$crumbs[1][0] = 'Publications';
    		$crumbs[1][1] = 'https://EXAMPLE.COM/publication/'; // URL of CPT slug
    		$crumbs[2][0] = get_the_title();
    	} else if ($post_type == 'news') {
    		$crumbs[1][0] = 'News';
    		$crumbs[1][1] = 'https://EXAMPLE.COM/news/'; // URL of CPT slug
    		$crumbs[2][0] = get_the_title();
    	}
    
    	return $crumbs;
    }, 10, 2);

    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

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

The ticket ‘Breadcrumb on ACF Posttypes’ is closed to new replies.