It’s no secret I use Avada theme on my site. Recently upgraded it to version 3.8.3 and was really happy that breadcrumb trail started showing category hierachy instead of just last leaf. However, when I yesterday posted about adding more instruments to ZynAddSubFX synthesizer, I noticed that breadcrumb on top of it was kind of weird:
Home > Linux > IT resources > Linux Mint > Adding more instruments to ZynAddSubFX
This is incorrect because Linux Mint category is child of Linux which itself is a child of IT resources. Because I wanted to give a try on Avada’s integrated breadcrumb engine (since it now relies on Data Vocabulary format) instead of WPML’s navigation plugin, I decided to trace the root of the problem.
First of all, let’s have sample hierarchy for testing – on my development environment made following:
- Root level
- 1st level
- 2nd level
- 3rd level
- 4th level
- 3rd level additional
- 3rd level
- 2nd level one more
- 2nd level
- 1st level
And one post named “Category hierarchy test post” under deepest hierarchy level – “4th level”. Avada version 3.8.3 breadcrumbs engine renders following:
Home / 3rd level / 2nd level / 1st level / Root level / 4th level / Category hierarchy test post
Which is messed. Or to be more precise, bolded path is reversed. Basically only “Home” and direct post category (“4th level”) is correct. Let’s trace issue by inspecting breadcrumbs file located at:
wp-content/themes/Avada/framework/class-breadcrumbs.php
Avada retrieves hierarchy trail in two places – functions get_post_terms and get_taxonomies call WordPress function get_ancestors. Thing is that it returns an array of ancestors from lowest to highest in the hierarchy, which in our case it is:
Array ( [0] => 23 [1] => 22 [2] => 21 [3] => 20 )
On my development environment these are:
- 23 – 3rd level
- 22 – 2nd level
- 21 – 1st level
- 20 – Root level
If printed out, this array would output reversed breadcrumb trail. That’s why we need to reverse arrays by adding following lines (highlighted) to the code in class-breadcrumbs.php:
In function get_post_terms (starts with line 371):
// If the term has a parent we need its ancestors for a full tree if ( $term_parent ) { // Get space separated string of term tree in slugs $term_tree = get_ancestors( $terms[0]->term_id, $taxonomy ); $term_tree = array_reverse($term_tree); $term_tree[] = get_term( $terms[0]->term_id, $taxonomy ); // Loop through the term tree foreach ( $term_tree as $term_id ) { // Get the term object by its slug $term_object = get_term( $term_id, $taxonomy ); // Add it to the term breadcrumb markup string $terms_markup .= $this->get_single_breadcrumb_markup( $term_object->name, get_term_link( $term_object ) ); } // We have a single term, so put it out } else { $terms_markup = $this->get_single_breadcrumb_markup( $terms[0]->name, get_term_link( $terms[0] ) ); }
And function get_taxonomies (starts with line 443):
// Make sure we have hierarchical taxonomy and parents if ( $term->parent != 0 && is_taxonomy_hierarchical( $term->taxonomy ) ) { $term_ancestors = get_ancestors( $term->term_id, $term->taxonomy ); $term_ancestors = array_reverse($term_ancestors); // Loop through ancestors to get the full tree foreach ( $term_ancestors as $term_ancestor ) { $term_object = get_term( $term_ancestor, $term->taxonomy ); $terms_markup .= $this->get_single_breadcrumb_markup( $term_object->name, get_term_link( $term_object->term_id, $term->taxonomy ) ); } }
The trick is performed by using PHP function array_reverse.
And voila – correct breadcrumb trail:
Home / Root level / 1st level / 2nd level / 3rd level / 4th level / Category hierarchy test post
With this success I’ve uploaded modified file to production environment as well – now my yesterday’s post has correct breadcrumb trail:
Home > IT resources > Linux > Linux Mint > Adding more instruments to ZynAddSubFX
I hope ThemeFusion (creators of Avada) will include this in next update. Happy breadcrumbing 🙂
Leave A Comment