• ResolvedModerator cubecolour

    (@numeeja)


    I am using WooCommerce with Storefront and a child theme, and the breadcrumbs are working great for the products.

    I have set the permalink for posts to be /blog/%category%/%postname%/ and this shows up as expected in the address bar for single posts and post archives, however I would like the breadcrumbs to reflect this structure also.

    I have a page set for the blog and when clicking the blog link on the menu, the breadcrumbs are:

    home > blog
    – this is perfect

    however when clicking on a category, eg mycat, The breadcrumb is:

    home > mycat
    – where I want this to be: home > blog > mycat

    Similarly on a single post, eg mypost, the breadcrumbs are

    home > mycat > mypost
    – I would like this to be: home > blog > mycat > mypost

    To summarise, when viewing a single post or a post archive I would like a link in the breadcrumbs to the ‘blog’ page.

    What would be the best way to achieve this?

    • This topic was modified 4 years ago by cubecolour.
Viewing 1 replies (of 1 total)
  • Moderator cubecolour

    (@numeeja)

    Having done some research I can now answer my own question.

    I came up with the following code to filter the breadcrumbs array, I’m posting it here in case anyone else has a similar question.

    
    /**
     * WooCommerce Breadcrumb
     * Add > blog > link to single posts and post archives
     *
     */
    function cc_add_blog_link_to_breadcrumbs( $crumbs, $breadcrumb ) {
    
    	// Check we are on a single post or a post archive
    	if ( ( is_singular( 'post' ) ) || ( is_post_type_archive( 'post' ) ) || ( is_category() ) || ( is_tag() ) || ( is_author() ) ) {
    
    		//* get the home link
    		$homelink = $crumbs[0];
    
    		//* add another home link at the front of the crumbs array - we can then change the original one
    		array_unshift($crumbs , $homelink);
    
    		//* get the link to the blog page
    		$blogpagelink = get_post_type_archive_link( 'post' );
    
    		//* replace the value of the second crumb with the blog page link
    		$crumbs[1][0] = 'blog';
    		$crumbs[1][1] = $blogpagelink;
    	}
    
       return $crumbs;
    }
    add_filter( 'woocommerce_get_breadcrumb', 'cc_add_blog_link_to_breadcrumbs', 20, 2 );
    
Viewing 1 replies (of 1 total)
  • The topic ‘Add ‘blog’ to breadcrumbs for single posts and post archives’ is closed to new replies.