Skip Navigation

[Resolved] Display Parent Taxonomy

This support ticket is created 7 years, 1 month 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.

Our next available supporter will start replying to tickets in about 1.13 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 5 replies, has 2 voices.

Last updated by Minesh 7 years ago.

Assisted by: Minesh.

Author
Posts
#506764

When user select, they are only allow to select child taxonomy, but we need to display both Parent & Child Taxonomy in the content.

Currently we are using this shortcode [wpv-post-taxonomy type="location" format="name"], but it only display the child taxonomy. Please let know

1. There is any shortcode can show both parent & child taxonomy?
2. There is any shortcode only show parent taxonomy?

#507140

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

As you can see with our Docs there is no option to display the taxonomy terms attached to post in the hierarchical format:
=> https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-taxonomy

1. There is any shortcode can show both parent & child taxonomy?
You can use the custom shortcode and adjust to your requirement - for example:
Please try to add following code to your current theme's functions.php file.

function list_hierarchical_terms($atts) {
    global $post;
     
     
    $taxonomy = $atts['taxonomy']; // change this to your taxonomy
    $separator = $atts['separator'];
    $terms = wp_get_object_terms( $post->ID, $taxonomy, array( "fields" => "ids" ) );
    if( $terms ) {
        $terms = trim( implode( ',', (array) $terms ), ' ,' );
        return wp_list_categories( 'title_li=&taxonomy=' . $taxonomy . '&include=' . $terms."&hierarchical=1"."&separator=".$separator."&echo=0&style=none");
         
    }
}
add_shortcode('display_post_tax_terms_hierarchical', 'list_hierarchical_terms');

User shortcode as:

[display_post_tax_terms_hierarchical taxonomy="vehicle-type" separator="/"]

Where:
Replace "taxonomy" with your taxonomy slug and separator as you want.

2. There is any shortcode only show parent taxonomy?
Please refer to following links that may help you:
=> http://stackoverflow.com/questions/19961130/wordpress-how-to-get-parent-category-id

You may also check following interesting category functions with number of parameters:
=> http://codex.wordpress.org/Function_Reference/get_categories
=> http://codex.wordpress.org/Function_Reference/get_category
=> https://codex.wordpress.org/Function_Reference/wp_list_categories

You can check and customize above shortcode as per your need and if you need custom programming for your site, please do not hesitate to contact our certified partners:
=> https://toolset.com/consultant/

#507161
Toolset Taxonomy Slug.JPG

My slug is "location" and I put below code into "my custom functions" plugin

function list_hierarchical_terms($atts) {
    global $post;
      
      
    $taxonomy = $atts['location'];
    $separator = $atts[','];
    $terms = wp_get_object_terms( $post->ID, $taxonomy, array( "fields" => "ids" ) );
    if( $terms ) {
        $terms = trim( implode( ',', (array) $terms ), ' ,' );
        return wp_list_categories( 'title_li=&taxonomy=' . $taxonomy . '&include=' . $terms."&hierarchical=1"."&separator=".$separator."&echo=0&style=none");
          
    }
}
add_shortcode('display_post_tax_terms_hierarchical', 'list_hierarchical_terms');

And put this shortcode into content template

[display_post_tax_terms_hierarchical taxonomy="location" separator=","]

But nothings happen, what have I done wrong? The taxonomy is created by toolset.

PS : I am sure "my custom functions" plugin is work.

#507164

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

As you are passing "separator" as attribute please use following code. Following line should remain as it is:

$separator = $atts['separator'];

So it should look like:

function list_hierarchical_terms($atts) {
    global $post;
       
       
    $taxonomy = $atts['location'];
    $separator = $atts['separator'];
    $terms = wp_get_object_terms( $post->ID, $taxonomy, array( "fields" => "ids" ) );
    if( $terms ) {
        $terms = trim( implode( ',', (array) $terms ), ' ,' );
        return wp_list_categories( 'title_li=&taxonomy=' . $taxonomy . '&include=' . $terms."&hierarchical=1"."&separator=".$separator."&echo=0&style=none");
           
    }
}
add_shortcode('display_post_tax_terms_hierarchical', 'list_hierarchical_terms');
#507254

Miniesh,

It is still not working. It show nothing.

We try to change the slug 'location' to 'taxonomy', the post shows only the child taxonomy. I suspect may be we put wrong taxonomy slug??

#507262

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

No - it should work like this because you need to adjust the code.

As I tried to explain there is no native shortcode available to display it with associated parent taxonomy terms.

If above custom shortcode displays the child terms, what you need to to is adjust the code and get parent term id based on child term ids and then combine array or child + parent terms and then try to display your shortcode.

Following link may help you to get parent category id or you may google it:
=> http://stackoverflow.com/questions/19961130/wordpress-how-to-get-parent-category-id

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