I have a taxonomy view which appears inside a search view. The search is showing posts, the taxonomy view in it is showing, separately, the taxonomy matching the current search term.
for example, searching for "wheels" will show all posts with "wheels" in the title, and also all taxonomies with "wheels" in the title.
so far so good, everything works.
what i want now is, inside the taxonomy view, to show not only the taxonomy name, but also it's parent (if it has one). the reason is I can have 2 taxonomies called "wheels", but the first's parent would be "objects" and the second's parent would be "circles" (just an example, these are not the actual names...), so I need to show that the first "wheels" is the child of "objects" and the second is the child of "circles"
i know i can create another taxonomy view with filter of "parents" set to "none", thus displaying the top taxonomies in the hierarchy, but I need to also relate it to the current taxonomy, which i can't get my head around...
thx!
Hi,
Thank you for contacting us and I'd be happy to assist.
To show the parent term's name in the loop of the taxonomy view, you'll need a custom shortcode, for example:
add_shortcode('show_tax_term_parent', 'show_tax_term_parent_func');
function show_tax_term_parent_func($atts) {
$taxonomy = $atts['taxonomy'];
$term_id = $atts['term_id'];
$current_term = get_term_by('ID', $term_id, $taxonomy, 'ARRAY_A');
if ( ($current_term) && ($current_term['parent'] != 0) ) {
$parent_term = get_term_by('ID', $current_term['parent'], $taxonomy, 'ARRAY_A');
return $parent_term['name'];
}
}
The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.
This shortcode can be used in the loop of the taxonomy view like this:
[show_tax_term_parent term_id="[wpv-taxonomy-id]" taxonomy="category"]
The attribute "term_id" expects the target term's ID which is passed using the shortcode "[wpv-taxonomy-id]" and the attribute "taxonomy" expects the slug of the target taxonomy, which in this example is "category".
Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
regards,
Waqar
thx Waqar!
Almost there... your code is providing the direct parent, whereas I need to get the highest parent. In that possible?
cheers
Ido
To get the grandparent term, the code for the shortcode will need to be updated:
add_shortcode('show_tax_term_parent', 'show_tax_term_parent_func');
function show_tax_term_parent_func($atts) {
$taxonomy = $atts['taxonomy'];
$term_id = $atts['term_id'];
$ancestors = get_ancestors( $term_id, $taxonomy );
if(!empty($ancestors)) {
$parent_term = get_term_by('ID', $ancestors[array_key_last($ancestors)], $taxonomy, 'ARRAY_A');
return $parent_term['name'];
}
}
My issue is resolved now. Thank you!