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
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/
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
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
My issue is resolved now. Thank you!