Hello @gorfeas,
Thank you for contacting Rank Math support.
Please use the following filter to modify the breadcrumbs for your CPT posts and taxonomies:
add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
$thrace = ['Thrace',
'https://wp.emthrace.org/thrace/'
];
$traditions = ['Traditions',
'https://wp.emthrace.org/thrace/traditions/'];
if(get_post_type() == 'tradition') {
array_splice( $crumbs, 1, 0, array($thrace) );
array_splice( $crumbs, 2, 0, array($traditions) );
}
return $crumbs;
}, 10, 2);
Here is how to add a filter to your site: https://rankmath.com/kb/wordpress-hooks-actions-filters/
Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.
Hello @gorfeas,
Please make sure that the post type’s name is correct in the filter. If the name of your post type is Traditions, you can use the following filter:
add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
$thrace = ['Thrace',
'https://wp.emthrace.org/thrace/'
];
$traditions = ['Traditions',
'https://wp.emthrace.org/thrace/traditions/'];
if(get_post_type() == 'traditions') { //replace with the name of the CPT
array_splice( $crumbs, 1, 0, array($thrace) );
array_splice( $crumbs, 2, 0, array($traditions) );
}
return $crumbs;
}, 10, 2);
Let us know how that goes. Looking forward to helping you.
That did it! it was that “s” missing. Thank you so much!
Hello @gorfeas,
We are super happy that this resolved your issue.
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.
Hi again,
is there any way I could make this work with my second language?
https://wp.emthrace.org/thrace/traditions/autumn/
breadcrumbs display correctly “Home / Thrace / Traditions / Autumn”
however when switching to greek
https://wp.emthrace.org/el/thraki/dromena/fthinoporo/
breadcrumbs display “Αρχική / Thrace / Traditions / Φθινόπωρο”
Is there a way to display the respective WPML translations? Or just add the translation via some kind of condition within the filter itself in order to display this instead? “Αρχική / Θράκη / Δρώμενα / Φθινόπωρο”
Hello @gorfeas,
One possibility would be to wrap the hard-coded entries for “Thrace” and “Traditions” in the WordPress translation function: https://developer.wordpress.org/reference/functions/__/
After that, it would be possible to include translations for those following the steps here: https://wpml.org/forums/topic/i-want-to-translate-something-through-code/
Hope this helps solve your issues.
Don’t hesitate to get in touch if you have any other questions.
I have managed to translate the strings after adding the following lines to the functions.php of my child theme, however breadcrumbs remain unchanged
$translated = __( 'Thrace', 'rnkmbrdc_thrace' );
$translated = __( 'Traditions', 'rnkmbrdc_trad' );
Hello @gorfeas,
You can use the following filter to set the breadcrumbs based on the current language:
add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
$thrace = ['Thrace',
'https://wp.emthrace.org/thrace/'
];
$traditions = ['Traditions',
'https://wp.emthrace.org/thrace/traditions/'];
$greek_thrace = ['Θράκη',
'https://wp.emthrace.org/el/thraki/'];
$greek_tradition = ['Δρώμενα',
'https://wp.emthrace.org/el/thraki/dromena/'];
if(get_post_type() == 'tradition') {
$my_current_lang = apply_filters( 'wpml_current_language', NULL );
if($my_current_lang == 'el') {
array_splice( $crumbs, 1, 0, array(($greek_thrace) ));
array_splice( $crumbs, 2, 0, array($greek_tradition) );
} else {
array_splice( $crumbs, 1, 0, array($thrace) );
array_splice( $crumbs, 2, 0, array($traditions) );
}
}
return $crumbs;
}, 10, 2);
Hope that helps. Please do not hesitate to let us know if you need our assistance with anything else.
I understand the conditional logic but unfortunately it doesn’t work
https://prnt.sc/4lbso19WkHyE (new snippet EGNLISH)
https://prnt.sc/14O4gsW-stEN (new snippet GREEK)
https://prnt.sc/oE6cyoIpBc7D (old snippet ENGLISH) works
Hello @grofeas,
Sorry, we again made the mistake in post type name in the filter. Please replace the tradition with traditions in the filter, and it should work fine:
add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) { $thrace = ['Thrace', 'https://wp.emthrace.org/thrace/' ]; $traditions = ['Traditions', 'https://wp.emthrace.org/thrace/traditions/']; $greek_thrace = ['Θράκη', 'https://wp.emthrace.org/el/thraki/']; $greek_tradition = ['Δρώμενα', 'https://wp.emthrace.org/el/thraki/dromena/']; if(get_post_type() == 'traditions') { $my_current_lang = apply_filters( 'wpml_current_language', NULL ); if($my_current_lang == 'el') { array_splice( $crumbs, 1, 0, array(($greek_thrace) )); array_splice( $crumbs, 2, 0, array($greek_tradition) ); } else { array_splice( $crumbs, 1, 0, array($thrace) ); array_splice( $crumbs, 2, 0, array($traditions) ); } } return $crumbs; }, 10, 2);
Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.
Ah! ok got it! That is just great! thank so much for your time and effort!
Hello @gorfeas,
Glad that helped.
If it isn’t too much to ask for, would you mind leaving us a review here?
https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post
It only takes a couple of minutes but helps us tremendously.
It would mean so much to us and would go a long way.
Thank you.