• Resolved Marc

    (@mdxfr)


    Hello Phi,

    We can use breadcrumb_block_single_prepend in case we want to alter the path to add for instance the blog home. It asks for 2 params, one is the $post.

    So we get Home > Blog > My post

    But what about for an archive page ? For example for the Post CPT

    The need is to have such a path for an archive page for posts :

    > Home > Blog > My Cat

    where my cat is the archive page, so we get the same path pattern accross the “blog” area.

    thanks for your help

    • This topic was modified 6 days, 4 hours ago by Marc.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Phi Phan

    (@mr2p)

    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 5 days, 22 hours ago by Marc.
    Plugin Author Phi Phan

    (@mr2p)

    @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 !

    Plugin Author Phi Phan

    (@mr2p)

    @mdxfr You’re welcome. I’m glad to hear it works.

    Phi.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.