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?
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/
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.
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');
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??
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