Hello Marc @mdxfr,
Could you clarify what you mean by “an archive page for posts”? Also, is “My cat” a post tag or a category? Please provide more details.
Thanks, Phi
Thread Starter
Marc
(@mdxfr)
hello,
My cat = just a post category page
an archive page for posts = the page that displays the post category (is_archive). Often we it dispays a query loop of posts for the given category in it + the category description.
In FSE, the breadcrumb has to be set in the Post Archive Template.
-
This reply was modified 1 year ago by
Marc.
@mdxfr Here is the code snippet for all single, category, post tag contexts:
// Prepend the blog page.
function breadcrumb_block_prepend_blog( $breadcrumbs_instance ) {
$blog_id = get_option( 'page_for_posts' );
if ( $blog_id ) {
$breadcrumbs_instance->add_item( get_the_title( $blog_id ), get_permalink( $blog_id ) );
}
}
// Single post context.
add_action(
'breadcrumb_block_single_prepend',
function ( $post, $breadcrumbs_instance ) {
if ( 'post' === $post->post_type ) {
breadcrumb_block_prepend_blog( $breadcrumbs_instance );
}
},
10,
2
);
// Category context.
add_filter(
'breadcrumb_block_get_item',
function ( $item, $context, $breadcrumbs_instance ) {
if ( ! $context || 'term' !== ( $context['type'] ?? '' ) ) {
return $item;
}
if ( 'category' === $context['object']->taxonomy && ! intval( $this_category->parent ) ) {
breadcrumb_block_prepend_blog( $breadcrumbs_instance );
}
return $item;
},
10,
3
);
// Post tag context.
add_filter(
'breadcrumb_block_get_item',
function ( $item, $context, $breadcrumbs_instance ) {
if ( ! $context || 'term' !== ( $context['type'] ?? '' ) ) {
return $item;
}
if ( 'post_tag' === $context['object']->taxonomy ) {
breadcrumb_block_prepend_blog( $breadcrumbs_instance );
}
return $item;
},
10,
3
);
I’ve not tested it yet, please let me know if it works for you.
Phi.
Thread Starter
Marc
(@mdxfr)
Hello,
i’ve tweaked a little bit :
- since the “Single post context.” part adds twice the blog root in a single post, i’ve deactivated this part.
- seems the category context covers already the case with if the IF statements
- in my context, the blog root page is a Page, so i’ve hard set the blog_id
just for anyone looking for this, now the breadcrumbs are
home / blog /// blog home
home / blog / cat /// blog cat archive page
home / blog / cat / mypost /// blog single post (considering a post is a related to a solely single category, i did not test in case of multiple categories associated to a single post)
thank Phi !
@mdxfr You’re welcome. I’m glad to hear it works.
Phi.