Skip Navigation

[Resolved] Displaying only the top-level taxonomies (not sub/children) on a custom post

This thread is resolved. Here is a description of the problem and solution.

Problem:
The wpv-post-taxonomy shortcode outputs all terms of a taxonomy regardless of hierarchy. How is it possible to only output the top-level terms and not the child terms?

Solution:
Custom code is required because the native get_the_terms function used by the wpv-post-taxonomy shortcode does not include a parameter for such a feature.

A custom shortcode to output only the top-level terms is shown in the reply below: https://toolset.com/forums/topic/displaying-only-the-main-taxonomies-not-subchildren-on-a-custom-post/#post-585379

This support ticket is created 7 years ago. 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 6 replies, has 2 voices.

Last updated by herreV 7 years ago.

Assisted by: Nigel.

Author
Posts
#585161

Hello,
I've created a CPT called company
And Custom Hierarchical Taxonomy called Company sector

The taxonomy has the following structure:
Main sector
- product

e.g.

Fruits & Vegetables
- Pineapple
- Green Breans

On the company template I would like to only display the main taxonmy... e.g. a company in in the Fruits & Vegetables sector, that exports Pineapple...
It should only display Fruits & Vegetables...
At this moment is shows F&V , but also Pineapple...

What is the quickest way to solve this?

#585316

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Herre

"At the moment it shows..."

Can you clarify where the taxonomy is being shown and how it is added?

You have created a template for single company posts, and you are outputting the taxonomy using the wpv-post-taxonomy shortcode, is that correct?

The shortcode doesn't have the option to show only the upper hierarchy term, but if that is what you are trying to do I should be able to help you do so.

#585330

Yes correct ... Its a single post template (using Divi Custom Post Type Layout Injector) but that probably has the same effect as when adding it to a normal single post template.

Indeed using the wpv-post-taxonomy shortcode

#585379

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Herre

The Views shortcode wpv-post-taxonomy uses the built-in WP function get_the_terms, and this does not accept any kind of parameter to indicate the depth in the hierarchy to retrieve terms.

To output just the top-level term it is necessary to create a custom shortcode which will effectively do the same as wpv-post-taxonomy but display only the top-level term.

You can add this to your theme's functions.php file (or use a plugin such as Code Snippets):

/**
 * Custom shortcode to output top-level term
 */
add_shortcode( 'top-term', function($atts=[]){

	global $post;

	$taxonomy = $atts['type'];

	$terms = get_the_terms( $post->ID, $taxonomy);

	foreach ($terms as $term) {

		if ( '0' == $term->parent ) {

			return $term->name;
		}
	}

	return '';
});

It simply outputs the name of the top-level term of the taxonomy you specify with the attribute type, e.g.

[top-term type="company-sector"]

Let me know how you get on.

#585768

Hi Nigel,
I was able to use and modify your piece of code.. thanks!

Can I ask one more question related to taxonomies... and views...

I use a View, that runs through the "company-sector" taxonomy, it uses a filter only disply items with no parent. I've set it also that when that it should only display when there are posts...

This all works as expected....!

So this generates a list with only the main parents... with some hrml stlyling I use this to create the submenu and added this to the Ubermenu plugin. This is something I use on much more site...

But now my issue...

This taxonomy is shared by two post types...
1) Products
2) Companies

The products are CPT that display export products, and do use the same taxonomy, so i can list featured cimpanies that do also export that product. Works like a charm....

It's not a big deal, but I'm curious if it is possible to adapt the view that lists the taxonomies in such a way it wol only disply the taxonomy when there is a product added to it...

At this moment it also displays when there is a company linked to it... But in the most perfect situation it should only display when there is also a product linked to the taxonomy.

E.g.
Product Pineapple
taxonomy -> Fruits & Vegetables / Pineapple

This should display Fruits & Vegetables in the menu

But in case i haven't created a Products Post linked to Pineapple yet, it shouldn't show up.

Hope this makes any sence

Regards
Herre

#585972

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Herre

I follow what you are trying to do, but it is not readily possible.

A Views taxonomy query is built on top of the get_terms function (https://developer.wordpress.org/reference/functions/get_terms/) and can only use the same arguments as the native function.

The possible arguments are described here: https://developer.wordpress.org/reference/classes/wp_term_query/__construct/

You'll see that the hide_empty argument is a simple boolean, it is not possible to specify which type of posts the term must be assigned to.

#585974

Hey Nigel,

No prob, I will dive into this info when I have more spare time 😉

Thanks for your quick reply.

Regards,
Herre