WooCommerce: Wrong breadcrumbs displayed

  • Author
    Posts
  • #24452
    thok

    Hello!

    I´m working on a website where I´m using posts that are in multiple categories. However, when I click on a menu item that leads to the correct lists of posts, the breadcrumbs show the first category of the first post displayed; but not the category I clicked on.

    That means, lets say I have several posts in both categories, “English” and “Something Else”. If I click on “Something Else” in the menu, the breadcrumb shows “Home -> English”; Just because the category “English” is alphabetically listed before “Something Else” and the first post is in both categories.
    This is confusing because the user didn´t click on “English”. Also, the URL still says “.\category\something-else”

    Is there a way to change that?

    Cheers!

    • This topic was modified 8 years ago by Zed. Reason: updated title for clarity
    #24600
    Zed
    Cryout Creations mastermind

    Wordpress has no way of knowing which links you clicked on to get to that post, so it displays the first category (alphabetically) in which the post is found.

    Posts shouldn’t be placed in several categories if you care about what your breadcrumbs display. Try this plugin and see if it helps you in any way.

    • This reply was modified 10 years ago by Zed.

    If you like our creations, help us share by rating them on WordPress.org.
    Please check the available documentation and search the forums before starting a topic.
    #25243
    David B

    When I set the site up in January with Mantra, the breadcrumbs were all correct.
    Now breadcrumbs are wonky on the blog only.
    The Blog page shows Domain >> Home (not Blog)
    If I open a specific post, it shows domain >> Books >> Postname

    It displays it this way if I click on the post title, a Recent posts list link, or the Continue Reading link. If I go into a category, it displays the path to that category and continues with the correct category if I open one of the posts in the Category list. If I open a post via Archives, it shows Books path above again.

    Books is the name of a Page in the top menu (not adjacent) and the name of my second category, not the first.
    At first I thought this may be a bug in Excerpts as I’d recently changed to using that. But when I turned that off, it didn’t change the above.

    Suggestions?

    #25244
    David B

    Apologies, I see this is a known issue in another thread (of several)

    #26053
    Jonathon J

    WordPress has no way of knowing which links you clicked on to get to that post, so it displays the first category (alphabetically) in which the post is found.

    The latter part of this statement is correct, the first part is debatable. I have this similar problem, where I have a product in WooCommerce that needs to be in a certain three categories, however, the same category is showing up in the breadcrumbs no matter the means of arriving at the product. I also needed the image of the category accessed from to display above the product description, so again I needed to determine from which category archive the product had been accessed by. Thankfully, WordPress does have a way to determine which link was used to access a page: wp_get_referer().

    wp_get_referer() returns the url of, you guessed it, the referring URL. In this case, it’d be the category archive page that the user was viewing. In order to achieve my goal, I gathered an array of the categories associated with the product/post, obtained the URL of the previous page, looped through each category and checked to see if the category slug existed in the referring URL. If false, then I continued through the loop. If true, then I could persist with the rest of my code to display the specific category image instead of all three at the same time or even the wrong one. I’m about to try applying this same principle to adjusting the breadcrumbs as well. I’ll let you know what I come up with. My code is below for reference if you think it’ll help.

    /**
    * Add Category image above a single course view
    **/
    add_action( 'woocommerce_before_single_product', 'woocommerce_brand_course', 0);
    function woocommerce_brand_course(){
        global $post;
        $terms = get_the_terms( $post->ID, 'product_cat' );
        $referer = wp_get_referer();
        if(!empty($terms)){
            foreach( $terms as $term){
                $referer_slug = (strpos($referer, $term->slug));
    
                if(!$referer_slug==false){
                    
                    $category_name = $term->name;
                    $category_thumbnail = get_woocommerce_term_meta( $term->term_id, 'thumbnail_id', true);
                    $image = wp_get_attachment_url($category_thumbnail);
                    echo '<div class="cat_bundle">';
                    echo '<img class="course-brand" src="'.$image.'">';
                    echo '</div>';
                }
                else{ }
            }
        }
    }
    #26055
    Jonathon J

    Yep, it worked. I know you weren’t specifically using WooCommerce, but I’ll post up my code anyway in case it helps you or someone else. I copied the WooCommerce templates/global/breadcrumb.php file over to my theme’s directory woocommerce/global/breadcrumb.php. Then I replaced the portion of their code for single pages that are of type ‘product’:

    elseif ( is_single() && ! is_attachment() ) {
    
    		if ( get_post_type() == 'product' ) {
    
    			echo $prepend;
    
    			if ( $terms = wc_get_product_terms( $post->ID, 'product_cat', array( 'orderby' => 'parent', 'order' => 'DESC' ) ) ) {
    
    				$main_term = $terms[0];
    
    				$ancestors = get_ancestors( $main_term->term_id, 'product_cat' );
    
    				$ancestors = array_reverse( $ancestors );
    
    				foreach ( $ancestors as $ancestor ) {
    					$ancestor = get_term( $ancestor, 'product_cat' );
    
    					if ( ! is_wp_error( $ancestor ) && $ancestor )
    						echo $before . '<a href="' . get_term_link( $ancestor->slug, 'product_cat' ) . '">' . $ancestor->name . '</a>' . $after . $delimiter;
    				}
    
    				echo $before . '<a href="' . get_term_link( $main_term->slug, 'product_cat' ) . '">' . $main_term->name . '</a>' . $after . $delimiter;
    
    			}
    
    			echo $before . get_the_title() . $after;
    

    with my code instead:

    elseif ( is_single() && ! is_attachment() ) {
    
    		if ( get_post_type() == 'product' ) {
    
    			echo $prepend;
    
    			if ( $terms = get_the_terms( $post->ID, 'product_cat' ) ) {
                    
                    $referer = wp_get_referer();
                    foreach( $terms as $term){
                        $referer_slug = (strpos($referer, $term->slug));
    
                        if(!$referer_slug==false){
                            $category_name = $term->name;
                            $ancestors = get_ancestors( $term->term_id, 'product_cat' );
    				        $ancestors = array_reverse( $ancestors );
                            
                            foreach ( $ancestors as $ancestor ) {
    					       $ancestor = get_term( $ancestor, 'product_cat' );
    
                                if ( ! is_wp_error( $ancestor ) && $ancestor )
                                    echo $before . '<a href="' . get_term_link( $ancestor->slug, 'product_cat' ) . '">' . $ancestor->name . '</a>' . $after . $delimiter;
                            }
                            echo $before . '<a href="' . get_term_link( $term->slug, 'product_cat' ) . '">' . $category_name . '</a>' . $after . $delimiter;
                        }
                    }
    			}
    
    			echo $before . get_the_title() . $after;

    Same concept as I mentioned before. I just had to replace their main_term variable to the referring term instead. Hope this helps!

    #26105
    Zed
    Cryout Creations mastermind

    I’m glad you found a solution.
    You should suggest this change to the Woocommerce team, as you’re editing plugin files to correct behaviour.
    I don’t believe we can bundle other plugins’ files to our theme.


    If you like our creations, help us share by rating them on WordPress.org.
    Please check the available documentation and search the forums before starting a topic.
    #29082
    Simon Vincent

    Do you think this would be possible for normal wordpress posts and/or custom post types?

    #30672
    Jarmonneke

    Jonathon J, Your solution worked perfect for a while, but not anymore, maybe is has to do with updates, but I don’t now when this problem occured: the trail showes like this now:
    Home / THUIS / Tuinmeubelen / Applebee tuinmeubelen / Applebee AIR lounge / THUIS / Tuinmeubelen / Applebee tuinmeubelen / THUIS / Tuinmeubelen Applebee Air SOFA

    When it should be:
    Home / THUIS / Tuinmeubelen / Applebee tuinmeubelen / Applebee AIR lounge / Applebee Air SOFA

    Is there a simple way to fix this?

    Thanks

    #31106
    Denis

    Jonathan,

    Have you managed to find a solution for this for breadcrumb.php version 2.3.0?

    #31150
    Evgeniy

    Hi Jonathon,

    can you find a solution for new version of WooCommerce breadcrumb.php? (2.3.0)
    In new version there is no such code that you posted above 🙁

    #31881
    Karl

    I would also be very interested in knowing if this can be done – we’d like to change breadcrumbs and also filter the WP menu ideally.

    We came across this plugin https://www.perfectseourl.com/ which does a lot of what we want to do (cleaner URL structure too), but find the site somewhat sketchy with a WooThemes theme/logo rip-off and no information about the people/company building the plugin, or the plugin past/future.

    #31925
    Karl

    We had another look at this and merged the code above with the new breadcrumb file. If anyone is interested, it’s here http://pastebin.com/0PtSbjFT

    After the $wrap_before, we cut in with Jonathon’s code, merging the is_single() && get_post_type() == ‘product’ ), so it only shows on single product pages, otherwise it uses the general WooCommerce breadcrumbs. Works well for us, perhaps it can help someone else.

    #32295
    Pauli

    After reading your post @karl, I tried, but this wasn’t working for me. I bought the Perfectseourl plugin two days ago hoping its all okay, and yesss it works! Haha.. Can’t explain how happy we are with our shop now! It wasn’t cheap, but seems like the best solution we tried the last months.. Even after updating, to the latest version 2.3.13 all perfect!

    #32376
    Matt

    Nice job on the updated solution @Karl. Very much appreciated!

    #33117
    GerBear

    I copied and pasted the code provided by karl:
    http://pastebin.com/0PtSbjFT

    No deal, it doesn’t work for me.

    Is there something I’m missing here?

    Still the product is in category 1 2 & 3.

    If the customer gets to the product via Category 3, the breadcrumb still shows category 1.

    #33126
    GerBear

    Correction to my issue here.

    I’m using Woocommerce, but the breadcrumbs are generated by the Yoast Seo plugin.

    Any ideas how to fix this, very frustrating and losing days on end over this.

    Thanks guys.

    GerBear

    #33974
    Julian

    @ GerBear @karl, it doesn’t work anymore.

    Can someone provide another solution?

    Thanks!

    #35496
    Kevin

    All use cache, so i guess this will never work? Is fragment caching needed to do this?

    #36501
    Joris Witteman

    I’ve created an updated version of breadcrumb.php that works better with multiple nested categories on a product: http://pastebin.com/raw/bemM8ZNF

    Paste this into a new file at /wp-content/themes/your-theme/woocommerce/global/breadcrumb.php

    #36504
    Daniel

    Great! Thank you man!

    #36515
    Zed
    Cryout Creations mastermind

    I strongly suggest creating a child theme to place that file in to avoid losing it on the next theme update.


    If you like our creations, help us share by rating them on WordPress.org.
    Please check the available documentation and search the forums before starting a topic.
    #36607
    veritas techsoft

    hi..plz help me i want to category name in variable but still call in woocommerce_breadcrumb(home) this function on product page plz check in this page http://tailord4u.com/product/dark-black-fabric-2/ …help me thank you in advance

Viewing 23 posts - 1 through 23 (of 23 total)

The topic ‘WooCommerce: Wrong breadcrumbs displayed’ is closed to new replies.