Help with code for custom breadcrumbs

#97541
  • Resolved Elias
    Rank Math free

    Hello,

    I hardly know programming. I’m trying to get the code to get the following:

    – Inject some personalized breadcrumbs for posts stored in a specific category.
    – The structure of the crumbs would be: Custom element 1> Custom element 2> Post title

    add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
    	if(is_category('ID-Cat')){
                    $value1[] = array(
    		'Custom Page1', 
                    'http://localhost/cutompage1', 
                    'hide_in_schema' => false
    		);
                    $value2[] = array(
    		'Custom Page2', 
                    'http://localhost/cutompage2', 
                    'hide_in_schema' => false
    		);
    		array_splice( $crumbs, 1, 0, $value ); 
    		return $crumbs; 
    	}
    	return $crumbs;
    }, 10, 2);

    I have tried with this, but nothing, not even the custom crumbs are printed in the posts of the indicated category.

    A help please!
    PS: I don’t even know who to ask
    PS 2: I am already using these two filters on the site:

    add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
    		$breadcrumb[] = array(
    			0 => 'Turismo León Provincia',
    			1 => '/leon-provincia/',
    		);
    		array_splice( $crumbs, 1, -5, $breadcrumb );
    
    		if(is_singular('post')){	
    		unset($crumbs[0]);
                    $crumbs = array_values($crumbs);
    		return $crumbs; 
    		}
    	return $crumbs;
    }, 10, 2);
    add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
    	if (is_page( '' )){
                    $value[] = array(
    		'Taberna Gaia', 
                    '/', 
                    'hide_in_schema' => false
    		);
    		unset($crumbs[1]);
    		array_splice( $crumbs, 0, -2, $value ); 
    		return $crumbs; 
    	}
    	return $crumbs;
    }, 10, 2);
Viewing 7 replies - 1 through 7 (of 7 total)
  • Alberto
    Rank Math business

    Hello,

    Thank you for contacting Rank Math and sorry for any inconvenience that might have been caused due to that.

    Could you check with this code?

    add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
    	if(is_category('ID-Cat')){
                    array_unshift($crumbs, array(
    		'Custom Page2', 
                    'http://localhost/cutompage2', 
                    'hide_in_schema' => false
    		));
    
                    array_unshift($crumbs,
                    $value1[] = array(
    		'Custom Page1', 
                    'http://localhost/cutompage1', 
                    'hide_in_schema' => false
    		));
                    
    		return $crumbs; 
    	}
    	return $crumbs;
    }, 10, 2);

    Looking forward to helping you. Thank you.

    ​​​​​​​

    Elias
    Rank Math free

    Nothing, it doesn’t work. Although in is_category (‘ID-Cat’) put the ID of the category (number 17, is_category (’17’)) the entries stored in the category still show the same breadcrumb structure as the rest, the one fixed with this other filter that I indicated in my first message:

    add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
    		$breadcrumb[] = array(
    			0 => 'Turismo León Provincia',
    			1 => '/leon-provincia/',
    		);
    		array_splice( $crumbs, 1, -5, $breadcrumb );
    
    		if(is_singular('post')){	
    		unset($crumbs[0]);
                    $crumbs = array_values($crumbs);
    		return $crumbs; 
    		}
    	return $crumbs;
    }, 10, 2);
    Alberto
    Rank Math business

    Hello,

    In that case, I think I would need to take a closer look at the settings. Please edit the first post on this ticket and include your WordPress logins in the designated Sensitive Data section.
    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/

    We really look forward to helping you.

    Elias
    Rank Math free

    Hello,

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

    Thank you.

    Michael Davis
    Rank Math pro

    Hello,

    I modified the code on your page to the following which seems to have worked:

    
    add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {		
    		if(in_category('17')){	
    			$breadcrumb[] = array(
    			0 => 'Custom Element 1',
    			1 => '/leon-provincia/',
    		);
    		$breadcrumb[] = array(
    			0 => 'Custom Element 2',
    			1 => '/leon-provincia/',
    		);
    		array_splice( $crumbs, 1, -5, $breadcrumb );
    	
    		unset($crumbs[0]);
              $crumbs = array_values($crumbs);
    		return $crumbs; 
    		}
    	return $crumbs;
    }, 10, 2);
    

    You will need to however add the correct URL and crumb test for the same

    Hope that helps. If you have any further question(s), please let us know. Thank you.

    Elias
    Rank Math free

    It seems that everything works, thanks!

    Alberto
    Rank Math business

    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.

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

The ticket ‘Help with code for custom breadcrumbs’ is closed to new replies.