Change link in breadcrumbs

#598935
  • Resolved Anonymous
    Rank Math free

    Hello,

    I want to change the link in rankmath breadcrumbs for blog post.
    So for the breadcrumbs: home/blog-page/post-name I want to replace the link for “blog-page” from “www.my-site/blog-page-generated-by-theme” by “www.my-site/custom-blog-page”.

    It’s possible ?

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

    Thank you for contacting Rank Math and bringing your concern to our attention.

    In this case, you can use and customize this filter to change the blog URL in Rank Math’s breadcrumbs (affects single post type):

    /**
     * Allow changing or removing the Breadcrumb items
     *
     * @param array       $crumbs The crumbs array.
     * @param Breadcrumbs $this   Current breadcrumb object.
     */
    
    add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
        if(get_post_type() == 'post') { //Change this to match the post type name
            $crumbs[1][1] = 'www.my-site/custom-blog-page'; //Change this to your actual website blog URL
        }
        return $crumbs;
    }, 10, 2);

    And here’s how you can add the filter to your site:
    https://rankmath.com/kb/wordpress-hooks-actions-filters/

    Hope that helps.

    Thank you.

    Anonymous
    Rank Math free

    Hello, works well thank you.
    Can you write me too a function to change the link of post category in the breadcrumbs. To do this with specific category.
    Thanks

    Hello,

    Glad that helped.

    Could you please confirm if you wanted to change the link of Category Name in this sample breadcrumbs?

    Home >> Blog >> Category Name >> Post Name

    You can also share the affected URL in the sensitive data section so we can check the breadcrumbs and link.

    Meanwhile, you can customize this filter to target a specific category (combined with the filter above):

    add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
    $categories = array_map( function ( $category ) {
    		return $category->slug;
    	}, get_the_category() );
    
        if(get_post_type() == 'post' || ! in_array( 'category-slug', $categories)) { //Change this to match the post type name and category slug
            $crumbs[1][1] = 'www.my-site/custom-blog-page'; //Change this to your actual website blog URL
            $crumbs[2][1] = 'www.my-site/new-category-slug';
        }
        return $crumbs;
    }, 10, 2);

    Let us know how this goes.

    Thank you.

    Anonymous
    Rank Math free

    Hello,
    Yes I want to change the link for the category name in the breadcrumbs. The function to change the link for the blog work perfectly but I want to change category name link too.
    For example for this breadcrumbs :
    Home >> Blog >> Category Name 1 >> Post Name
    I want to say when category slug == “category-name-1” (or ID) change the link to “wwww.my-site/blog/category-name1/”
    And I want to do this for mutiple category name.
    Can you help me? thanks

    Hello,

    For the category you may use the below filter that searches for the specific category you want and replaces the slug of the category in breadcrumbs:

    add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
    	$categories = array_map( function ( $category ) {
    		return $category->slug;
    	}, get_the_category() );
    	
    	$category_slug = 'category-slug'; //Change the slug of the Category
    
        if( in_array( $category_slug, $categories)) {
    		
    		$url = get_category_link(get_category_by_slug($category_slug)->term_id);$i=0;
    		  while(count($crumbs) >= $i){
    			  if($url == $crumbs[$i][1]){
    				  $crumbs[$i][1] = 'www.my-site/blog/category-name1/'; //Set the desired/new URL here
    			  }$i++;
    		  }
        }
        return $crumbs;
    }, 10, 2);

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

    Thank you.

    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 5 replies - 1 through 5 (of 5 total)

The ticket ‘Change link in breadcrumbs’ is closed to new replies.