Skip Navigation

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

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

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:

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.

This topic contains 6 respuestas, has 2 mensajes.

Last updated by herreV hace 4 años, 2 meses.

Assigned support staff: Nigel.


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

The taxonomy has the following structure:
Main sector
- product


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?



Languages: Inglés (English ) Español (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.


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



Languages: Inglés (English ) Español (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.


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.

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




Languages: Inglés (English ) Español (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 ( and can only use the same arguments as the native function.

The possible arguments are described here:

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.


Hey Nigel,

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

Thanks for your quick reply.