Skip Navigation

[Resolved] Display different parts of Custom Taxonomy separately

This support ticket is created 5 years, 4 months 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.

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

This topic contains 2 replies, has 2 voices.

Last updated by Ben 5 years, 4 months ago.

Assisted by: Waqar.

Author
Posts
#1157929

Ben
Views Output.png
Locations.png

I would like to be able to display different terms of a Custom Taxonomy separately.

At the moment I have a taxonomy called Locations. I have the terms set out like this: Country, City, Area. So in my example I have: United Kingdom, London, Islington.

I can display these on the Front End using a View. I am currently using the below to do this.

[wpv-taxonomy-link]
 <br>
[types termmeta='flag' title='%%TITLE%%' alt='%%ALT%%' size='full'][/types]

You can see from my View Output screenshot that all the taxonomy terms display together.

Is there any way I can display each term separately? For example, display the Area and City together and then display the Country separate somewhere else?

#1158659

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Ben,

Thank you for contacting us and I'll be happy to assist.

There is no built-in shortcode available to show taxonomy terms, separated based on the hierarchy level.

To achieve this, you can register a custom shortcode, that gets all the terms attached to a post and then only show them, based on the provided "level" attribute.

The following code can be added to the active theme's "functions.php" file:


add_shortcode('get_terms_by_level', 'get_terms_by_level_func');
function get_terms_by_level_func($atts) {
     
	$taxonomy = $atts['taxonomy'];
	$postid = $atts['id'];
	$level = $atts['level'];

	if( !(isset($level)) || (empty($level)) ) {
		$level = 1;
	}

	$terms = get_the_terms( $postid, $taxonomy);

	if ( $terms && ! is_wp_error( $terms ) ) {

		foreach ($terms as $term) {

			if($term->parent == 0) {
				$results['1'] = array('id' => $term->term_id, 'name' => $term->name, 'slug' => $term->slug, 'parent' => $term->parent);		
			} else {
				$tmpresults[] = array('id' => $term->term_id, 'name' => $term->name, 'slug' => $term->slug, 'parent' => $term->parent);
			}

		}

		foreach ($tmpresults as $result) {
			if($result['parent'] == $results['1']['id']) {

				$results['2'] = array('id' => $result['id'], 'name' => $result['name'], 'slug' => $result['slug'], 'parent' => $result['parent']);
				
			}
			else {
				$results['3'] = array('id' => $result['id'], 'name' => $result['name'], 'slug' => $result['slug'], 'parent' => $result['parent']);
			}
		}

		return '<a href="' . esc_url( get_term_link( $results[$level]['id'], $taxonomy) ) . '">' . $results[$level]['name'] . '</a>';

	}
   
}

After that, in your view, you can use this new shortcode to show terms at different levels like this:


// get country level term with level 1
[get_terms_by_level id="[wpv-post-id]" taxonomy="category" level="1"]

// get city level term with level 2
[get_terms_by_level id="[wpv-post-id]" taxonomy="category" level="2"]

// get area level term with level 3
[get_terms_by_level id="[wpv-post-id]" taxonomy="category" level="3"]

Note: Please replace "category" with the actual slug of your custom taxonomy.

I hope this helps and please let me know if you have any questions around this approach.

regards,
Waqar

#1162075

Ben

Thank you so much for your help with this Waqar!

This works perfectly and allows me to achieve exactly what I wanted to do, with minimal fuss.

Your help is highly appreciated. Once again, thank you my friend!

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