-
AuthorPosts
-
March 19th, 2014 at 16:31 #24452thok
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
March 24th, 2014 at 14:03 #24600ZedCryout Creations mastermindWordpress 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.May 16th, 2014 at 07:49 #25243David BWhen 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 >> PostnameIt 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?
May 16th, 2014 at 07:51 #25244David BApologies, I see this is a known issue in another thread (of several)
July 8th, 2014 at 19:47 #26053Jonathon JWordPress 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{ } } } }
July 8th, 2014 at 20:18 #26055Jonathon JYep, 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!
July 15th, 2014 at 10:31 #26105ZedCryout Creations mastermindI’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.January 12th, 2015 at 18:58 #29082Simon VincentDo you think this would be possible for normal wordpress posts and/or custom post types?
February 26th, 2015 at 10:34 #30672JarmonnekeJonathon 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 SOFAWhen it should be:
Home / THUIS / Tuinmeubelen / Applebee tuinmeubelen / Applebee AIR lounge / Applebee Air SOFAIs there a simple way to fix this?
Thanks
April 16th, 2015 at 20:38 #31106DenisJonathan,
Have you managed to find a solution for this for breadcrumb.php version 2.3.0?
April 22nd, 2015 at 10:29 #31150EvgeniyHi 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 🙁May 20th, 2015 at 16:21 #31881KarlI 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.
May 22nd, 2015 at 17:40 #31925KarlWe 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.
July 11th, 2015 at 10:08 #32295PauliAfter 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!
July 25th, 2015 at 07:07 #32376MattNice job on the updated solution @Karl. Very much appreciated!
August 18th, 2015 at 04:27 #33117GerBearI copied and pasted the code provided by karl:
http://pastebin.com/0PtSbjFTNo 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.
August 19th, 2015 at 02:26 #33126GerBearCorrection 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
October 23rd, 2015 at 10:17 #33974JulianFebruary 5th, 2016 at 10:41 #35496KevinAll use cache, so i guess this will never work? Is fragment caching needed to do this?
April 20th, 2016 at 15:49 #36501Joris WittemanI’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
April 21st, 2016 at 18:13 #36504DanielGreat! Thank you man!
April 22nd, 2016 at 17:56 #36515ZedCryout Creations mastermindI 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.May 4th, 2016 at 08:32 #36607veritas techsofthi..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
-
AuthorPosts
The topic ‘WooCommerce: Wrong breadcrumbs displayed’ is closed to new replies.