Hello,
in my x Theme the breadcrumbs only show home -> post not home -> category -> post. How can I change this?
Hello,
in my x Theme the breadcrumbs only show home -> post not home -> category -> post. How can I change this?
Hey @Gamer1,
Thanks for writing in!
By default, the breadcrumb will display home -> post. The category name were not included in the breadcrumb.
To display the category, please check out this thread:
Hope this helps.
This dont work at my theme in the functions.php:
add_filter('x_breadcrumbs_data', 'post_parent_trail', 9999999, 2);
function post_parent_trail ($crumbs, $args) {
if ( is_singular('post') ) {
$category = get_the_category( get_the_ID() );
if ( count( $category ) > 0 ) {
$crumbs[1]['label'] = $category[0]->name;
$crumbs[1]['url'] = get_category_link( $category[0]->term_id );
unset($crumbs[2]); //to make sure category won't duplicate
}
}
return $crumbs;
}
Hi @Gamer1,
I tested the above code in my local setup by pasting the above code in my child theme’s functions.php file and it works as expected. Please make sure you’ve child theme activated and the code is placed in your child theme’s functions.php file.
If your setup is correct then you can replace the previous code with the following one:
add_filter('x_breadcrumbs_data', 'post_parent_trail', 9999999, 2);
function post_parent_trail ($crumbs, $args) {
if ( is_singular('post') ) {
$category = get_the_category( get_the_ID() );
if ( count( $category ) > 0 ) {
$crumbs[] = array(
'type' => 'category',
'url' => get_category_link( $category[0]->term_id ),
'label' => $category[0]->name,
);
$crumbs[] = array(
'type' => 'post',
'url' => get_post_permalink( get_the_ID() ),
'label' => get_the_title(get_the_ID()),
);
unset($crumbs[2]); //to make sure category won't duplicate
}
}
return $crumbs;
}
It will give you the format Home > Blog > Category > Post Name
e.g:
Let us know how this goes!
This code in my child theme functions.php cause a 500 HTTP Error. The other code below too.
<?php //add child theme add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' ); function theme_enqueue_styles() { wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' ); } add_filter('x_breadcrumbs_data', 'post_parent_trail', 9999999, 2); function post_parent_trail ($crumbs, $args) { if ( is_singular('post') ) { $category = get_the_category( get_the_ID() ); if ( count( $category ) > 0 ) { $crumbs[1]['label'] = $category[0]->name; $crumbs[1]['url'] = get_category_link( $category[0]->term_id ); unset($crumbs[2]); //to make sure category won't duplicate } } return $crumbs; } ?>Hi @Gamer1,
The first part seems not included on the default child theme code.
//add child theme
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() { wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' ); }
You can download the child theme from here.
For parent stylesheet, we just need this line. Check the default child theme.
// Enqueue Parent Stylesheet
// =============================================================================
add_filter( 'x_enqueue_parent_stylesheet', '__return_true' );
Then no need for closing php tag at the end ?>
500 error might means fatal error. You might check your server logs for the details of the error.
Always this code:
add_filter(‘x_breadcrumbs_data’, ‘post_parent_trail’, 9999999, 2);
function post_parent_trail ($crumbs, $args) {
if ( is_singular(‘post’) ) {
$category = get_the_category( get_the_ID() );
if ( count( $category ) > 0 ) {
$crumbs[1]['label'] = $category[0]->name;
$crumbs[1]['url'] = get_category_link( $category[0]->term_id );
unset($crumbs[2]); //to make sure category won’t duplicate
}
}
return $crumbs;
}
and this code:
add_filter(‘x_breadcrumbs_data’, ‘post_parent_trail’, 9999999, 2);
function post_parent_trail ($crumbs, $args) {
if ( is_singular(‘post’) ) {
$category = get_the_category( get_the_ID() );
if ( count( $category ) > 0 ) {
$crumbs[] = array(
'type' => 'category',
'url' => get_category_link( $category[0]->term_id ),
'label' => $category[0]->name,
);
$crumbs[] = array(
'type' => 'post',
'url' => get_post_permalink( get_the_ID() ),
‘label’ => get_the_title(get_the_ID()),
);
unset($crumbs[2]); //to make sure category won't duplicate
}
}
return $crumbs;
}
cause the 500 HTTP Error. If I delete that code the child theme works.
In the server logs I see nothing
Now the 500 HTTP Error don’t appears with the code above, but the breadcrumb has not changed and stay the same as before.
Hi @Gamer1,
Thank you for clarification. In that case, it happens because you are calling the same function twice. That will definitely cause an error. Please only try the code suggested by Nabeel above.
Hope this helps.
Hi Lely,
I mean every code as his own dont work. It does not matter if I use the first code or the second code. I dont have used both at the same time.
Hey @Gamer1,
The code is working for us. Please watch this screencast: https://youtu.be/VhXScWb2zC0. If that doesn’t work on your end, you most probably
Thank you for your cooperation.
You’re using an old version of X that’s not yet using the breadcrumbs filter. Please backup your site and update X and Cornerstone to the latest version. -> This is it. Thanks!
Glad this is now sorted out. Thank you for updating us.
This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.