[Gelöst] Displaying only the top-level taxonomies (not sub/children) on a custom post
Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.
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.
This support ticket is created vor 7 Jahren. 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.
Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.
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...
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.
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.
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.