How to show term slug in dynamic data

I’m trying to output the term slug in a taxonomy archive template.

Dynamic data for terms allows us to access the following:

How can I get access to the term slug using dynamic data?

2 Likes

I’m struggling with this as well! I ended up using a little shortcode solution, but it meant having to use a short code component instead of my intended link in the UI:

add_shortcode( ‘return_taxonomy_slug’, ‘my_shortcode_return_taxonomy_slug’ );
function my_shortcode_return_taxonomy_slug($atts, $content=’’) {
$term_object = get_term( $content );
return $term_object->slug;
}

As a workaround, I added a code section with:

<?php

$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$slug = $term->slug;

?>

Then I was able to echo the slug variable using:

<?php echo $slug; ?>

Would be super useful if this was built into Bricks. Seems odd to include ID, name, URL, description as standard but not include slug as a built-in option?

Define this custom function:

function bl_get_current_term_slug() {
	$term = get_queried_object();

	return $term->slug;
}

then use it like this:

{echo:bl_get_current_term_slug}
2 Likes

This only seems to work when one uses the

{echo:bl_get_current_term_slug}

in an archive template set for the taxonomy.

What if one wants to display the term slug on a regular page, inside a query loop? Asking because this doesn’t seem to work in that case :confused:

Thank you!

Try this function:

function bricks_get_term_slug()
{
    $looping_query_id = \Bricks\Query::is_any_looping();

    if (!empty($looping_query_id)) {
        $object = \Bricks\Query::get_loop_object($looping_query_id);
    }

// Is tax archive?
    if (empty($object) && is_tax()) {
        $object = get_queried_object();
    }

// Not a WP_Term, leave
    if (empty($object) || !is_a($object, 'WP_Term')) {
        return '';
    }

    return $object->slug;

}

Thanks! Do I add this code as an additional one to the one that Sridhar shared, or just use this one on it’s own? I added it, but how would this work in the query loop builder?

If I add a custom field to my taxonomy terms, I can choose it as dynamic data, but nothing gets dispayed…It only displays fine on an archive template for the particular taxonomy. But I need to display it on a regular page, inside the query loop builder for each post.

You would use this code instead of the code that Sridhar posted. Usage would be: {echo:bricks_get_term_slug} inside of a query loop.

As for the custom field, try {echo:bricks_get_term_field('FIELD_NAME_HERE')} with the function below (not tested):

function bricks_get_term_field( $field = '' )
{
// Field is empty, leave
    if ( $field == '' ) {
        return '';
    } 

    $looping_query_id = \Bricks\Query::is_any_looping();

    if (!empty($looping_query_id)) {
        $object = \Bricks\Query::get_loop_object($looping_query_id);
    }

// Is tax archive?
    if (empty($object) && is_tax()) {
        $object = get_queried_object();
    }

// Not a WP_Term, leave
    if (empty($object) || !is_a($object, 'WP_Term')) {
        return '';
    }

    return get_term_meta($object->term_id, $field, true);
}

Thanks for your help, I just tried your first code, but it doesn’t show anything.

I have three taxonomies for the post that is queried. The {echo:bricks_get_term_slug} is probably too broad.

I’ve updated the code in the previous post and tested it successfully on my end.

Hmm…In a query loop?

The steps I did:

  1. Add your code to my code snippets plugin
  2. go to my regular page, edit with Bricks, and add a basic text with {echo:bricks_get_term_field(‘my_custom_term_field’)} inside of the query loop (which queries a custom post type that has the taxonomy tied to it).

Return nothing.

It does work in an archive page made for the particular taxonomy. Only there.

Can you provide specifics of what you are trying to do?

Example:

CPT: testimonial

Custom taxonomy: testimonial_type

Sample terms for a given testimonial CPT item: audio_testimonial, video_testimonial

Custom field for testimonial_type taxonomy: testifier_name

Requirement: In the CPT’s Query Loop on a static Page, output the value of the testifier_name custom field for each testimonial_type term separated by commas in each post’s query loop iteration.