Skip Navigation

[Résolu] display taxonomy's parent in taxonomy view

This support ticket is created Il y a 2 années et 2 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Auteur
Publications
#2257609

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!

#2258077

Waqar
Supporter

Languages: Anglais (English )

Timezone: Asia/Karachi (GMT+05:00)

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

#2258331

thx Waqar!
Almost there... your code is providing the direct parent, whereas I need to get the highest parent. In that possible?
cheers
Ido

#2259033

Waqar
Supporter

Languages: Anglais (English )

Timezone: Asia/Karachi (GMT+05:00)

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'];
	}
}

#2259045

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.