Skip Navigation

[Resolved] Shortcode for Taxonomy name using slug as identifier

This support ticket is created 5 years, 5 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 4 replies, has 2 voices.

Last updated by mattL-11 5 years, 5 months ago.

Assisted by: Waqar.

Author
Posts
#1267803

Tell us what you are trying to do?
We'd like to be able to render the Name of Taxonomy labels on search views and other areas, by referencing the slug of that taxonomy. It looks like it's possible from previous tickets, but the solution urls aren't accessible.

<label>[show the name of taxonomy with slug:filter1]</label>
[wpv-post-taxonomy type="filter1"]

Is there any documentation that you are following?
https://toolset.com/forums/topic/custom-shortcode-for-taxonomy-name/#post-1120696

#1268975

Hi,

Would it be possible to get an update on this, I've tried the following solutions which look exactly what we're trying to achieve but it's not rendering the taxonomy name.

https://toolset.com/forums/topic/taxonomy-label-in-a-view/
https://toolset.com/forums/topic/my-taxonomy-label-shortcake-doesnt-show-name-of-taxonomy-anymore/

#1269101

Hi Matt,

I apologize for the delay, but I'm in the middle of some tests around this.

Will update you with my results, shortly.

Thank you for your patience.

regards,
Waqar

#1269195

Hi Matt,

Thank you for waiting.

During troubleshooting, I noticed that other support threads that you've mentioned were using "get_taxonomy" function, which uses the "name" and not the "slug".
( ref: https://codex.wordpress.org/Function_Reference/get_taxonomy )

To get the title/label of a Taxonomy using its slug, you can use "get_taxonomies" function ( ref: https://codex.wordpress.org/Function_Reference/get_taxonomies ), to first get all the taxonomies and then only show the title/label, if the provided slug, matches.

Example:


add_shortcode('taxonomy-label-by-slug', 'taxonomy_label_by_slug_func');
function taxonomy_label_by_slug_func($atts, $content){
	$atts = shortcode_atts( array(
		'slug' => '',
	), $atts );

	$args = array(
		'public'   => true,
		'_builtin' => true
	);

	$output = 'objects';
	$operator = 'or';

	// get all taxonomies
	$taxonomies = get_taxonomies( $args, $output, $operator );

	foreach ($taxonomies as $taxonomy) {
		// if the provided slug matches, save and return the Title/Label
		if ($taxonomy->name == $atts['slug']) {
			$label = $taxonomy->label;
		}	
	}

	return $label;
}

The shortcode can be used with a slug like this:


[taxonomy-label-by-slug slug="taxonomy-slug"]

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1269209

My issue is resolved now. Thank you!