Add dynamic color to taxonomy labels (Elementor + ACF)

David Denedo

Updated on April 27, 2024.

Table of Content

Introduction

Creating a visual distinction among various groups of loop items can significantly improve the user experience on your website. This tutorial will guide you on how to add coloured labels to your loop grids. Their taxonomy term will serve as the basis for the labels. This can be especially useful to distinguish between free and paid courses. It can also help to differentiate between different tiers of premium services that your company offers.

This tutorial uses Elementor Pro, Advanced Custom Fields (ACF), and a code snippets plugin or child theme. Elementor provides a range of features. These include flexbox containers, loop grids, loop carousels, and taxonomy filters. However, there’s room for improvement, especially in expanding the dynamic tags.

In this tutorial, we’ll use Elementor Pro to create the loop grid. We’ll use ACF to create the custom post type (CPT) and custom taxonomy. We’ll also use ACF to create the colour picker meta field for the taxonomy label colour.

Check out the tutorial video and share your thoughts in the comment section below.

PHP Code Snippet

Below is the PHP snippet used to create the [cat-color] shortcode. It dynamically retrieves the colour meta field “resource-type-color” within the custom taxonomy “resource-type” for the current post:

function cat_color_shortcode() {
    global $post;
    $post_id = $post->ID;
    $term_list = wp_get_post_terms($post_id, 'resource-type', array('fields' => 'ids'));
    $term_id = (int)$term_list[0];
    $color = get_field('resource-type-color', 'term_'.$term_id);
    if($color) {
        return $color;
    } else {
        return '';
    }
}
add_shortcode('cat-color', 'cat_color_shortcode');

Updated PHP Code Snippet

Below is an improved PHP Snippet. It is more customisable and useful when you want to display many term meta fields on your page. The syntax for the shortcode is [dd-term-meta tax="your-taxonomy-name" field="your-term-meta-field"]. Use the shortcode widget or the shortcode dynamic tag to add the term meta field to your page.

<?php
function dd_term_meta_shortcode($atts) {
    // Extract shortcode attributes
    $atts = shortcode_atts(
        array(
            'tax'   => 'category',          // Default taxonomy
            'field' => '',    // Default field
        ),
        $atts,
        'dd-term-meta'
    );

    global $post;
    $post_id = $post->ID;

    // Taxonomy Info
    $dd_taxonomy_slug = sanitize_text_field($atts['tax']);
    $dd_term_meta_field = sanitize_text_field($atts['field']);

    // Get an array of term IDs
    $term_list = wp_get_post_terms($post_id, $dd_taxonomy_slug, array('fields' => 'ids'));

    // Check if there are terms
    if (!empty($term_list)) {
        $term_id = (int) $term_list[0];

        // Get term meta using ACF
        $dd_term_meta = get_field($dd_term_meta_field, 'term_' . $term_id);

        // Check if term meta exists
        if ($dd_term_meta) {
            return $dd_term_meta;
        } else {
            return '';
        }
    }

    return '';
}

add_shortcode('dd-term-meta', 'dd_term_meta_shortcode');

Adding the shortcode as custom attribute

Elementor does not yet offer the option to use a shortcode as a dynamic tag for the background colour. Unfortunately. To overcome this limitation, we’ll use the Custom Attribute instead. Follow the steps below:

  1. Navigate to the Advanced Tab of the required heading widget.
  2. In the “Attributes” accordion, click on the dynamic tag icon.
  3. Choose the “Shortcode” dynamic tag, and click on the wrench icon.
  4. Insert the shortcode [cat-color] under the Settings accordion.
  5. Under the Advanced Accordion, in the “Before” input, add the following code: style|background-color:
  6. Save and exit.
Click on the dynamic tag under the Attributes Section
Add the shortcode
Add the before text

If you’ve followed these steps correctly, you’ll successfully apply coloured labels to your loop grid.

0 0 votes
Article Rating
Subscribe
Notify of
guest
21 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Don
Don
27 July 2023 5:43 am

How did you add the filtering for the loop , by another plugin or using beta version taxonomy filter?

Jeldo
Jeldo
23 September 2023 5:20 pm

Thank you! This was such a life-saver for me today!

Don
Don
27 July 2023 5:45 am

How did you add the filtering to the loop with a another plugin?

David
David
25 December 2023 5:20 pm

Thanks for the video.
I try to use this snippet php, for a heading element in a single-post page, but it does not work.
Maybe I need another code?

David
David
26 December 2023 9:52 am
Reply to  David Denedo

Hi,
Exactly what you did.
I created ACF with the name: “category-color”, for the field of the color of the category. Taxonomy with the name: “project-type”.
I changed your PHP code to match my names.
After that, I set a shortcode in Elementor properties.

This is my code:
function cat_color_shortcode() {
global $post;
$post_id = $post->ID;
$term_list = wp_get_post_terms($post_id, ‘project-type’, array(‘fields’ => ‘ids’));
$term_id = (int)$term_list[0];
$color = get_field(‘category-color’, ‘term_’.$term_id);
if($color) {
return $color;
} else {
return ”;
}
}
add_shortcode(‘cat-color’, ‘cat_color_shortcode’);

Todd
19 January 2024 7:43 pm

Very cool functionality. Thanks for the tutorial. Can a similar strategy be used to display images, Google Maps, etc.? As a summary:

I’m creating a site for my musician son with a custom ACF CPT called “Gigs” to display his upcoming shows. I’ve also created a custom ACF Taxonomy called “Locations” to enable a list of venues to be attached to the “Gigs” CPT. The “Locations” taxonomy has its own ACF custom fields (Image, Google Map, etc.) that would be equivalent to this “category-color” example.

That said, I cannot figure out how to display these other types of fields. I guess they are called “terms_meta” because they are fields that reside inside the taxonomy. I’ve tried code snippets that either don’t work or crash the site. I’m not developer, but rather a Beginner Dad Designer that is trying to create a beautiful and, most importantly, highly functional dynamic data driven site with minimal plugins.

Your insight is greatly appreciated. Thanks in advance!

Todd
20 January 2024 3:16 am
Reply to  David Denedo

David — Thanks for the quick reply. I look forward to hearing more about the Dynamic.ooo Shortcode plugin — although I’m really trying to minimize plugins … perhaps too much, I don’t know!

And, an update to the current “cat-color” code snippet would be amazing! I’ll stay tuned.

Cheers!

Todd
22 January 2024 9:21 pm
Reply to  David Denedo

David,
This is awesome! I have it working to pull in an image and URL for my locations. Haven’t figured out the ACF Google Map yet — likely more complex because that custom field is returning an array of data (latitude, longitude, zoom, address, …). I watched your new video as well & learned even more. Until watching that I didn’t realize that the attributes were actually editable in the Shortcode itself. I placed that data into the snippet. That works nicely too though because I could combine 2 fields into one Shortcode. For reference, I combined my Location Image and Location Link (URL) into a clickable image for the Location assigned to a “Gig” (essentially a music show). Here’s the code I plugged into the Shortcode Widget:


• dd-term meta returns the Location Link
• img-term-meta returns the Location Image
• both are inside the tag to create the link to the Location’s website from its image.

Very happy with this! I’ve been banging my head against the wall trying to figure out how to display custom fields inside my custom taxonomies! Until now!!!

I see that you tweaked the code a bit more in your video from what you linked above. I look forward to seeing that and diving in even deeper.

And finally, I totally agree with a commenter on your YouTube Channel — you deserve far more subscribers! Your stuff is great!

Cheers!
Todd

Pedro Simao
Pedro Simao
1 April 2024 3:06 pm

Hi David,

Great tutorial! Thank you so much for sharing.
I was wondering how I could target the div’s element to change the text color.
Any ideas?

Thanks in advance

Alex
11 April 2024 8:16 am

Hi David,

just a quick THANK YOU for this tutorial, it worked really well!
I’ve never even thought of putting something like “style|background-color:” in the “Before” input section there. This will be very useful in many cases!

Cheers from Berlin
Alex

Web guy
Web guy
23 May 2024 12:34 am

First of David, THANK YOU for this PHP snippet and all the videos you make. You are a credit to our community.

Here’s what I’ve done so far.

I’ve created a ACF custom post type Events.
I created 2 ACF Taxonomies (Event Categories and Event Venues) linked to the Events custom post type.
1. The Event Categories doesn’t have any custom fields (are these the meta fields you mentioned in your video?). I just use it as a post category but for events. E.G. Webinar, Training etc.
2. The Event Venues Taxonomy also has some custom/Meta fields (Event Venue Address, Event Venue City).
I’ve created an Elementor Loop Grid, which displays the Event post types fine.

I’ve used your updated snippet to resolve one issue;
I can now display the Event Venue Address and Event Venue City (again, thank you!).

But I can’t work out how to display the Event Category name (as it’s not a field within the Taxonomy, just the Taxonomy name(s) that were selected for that Event post).
I tried using the Elementor dynamic ‘Post Terms’ but when I click the wrench icon, the Taxonomy dropdown doesn’t have any options to choose from.

Web guy
Web guy
23 May 2024 6:21 am
Reply to  David Denedo

Thanks!

Sorry, another question. How do I display the Taxonomy Name in the loop item?

So, what I have is an Event Venue ACF taxonomy (linked to the Events ACF custom post type), with Event Address and Event City meta fields.
Let’s use this as an example;
• ‘Amazing Venue 1’ (this the Event Venue Name and is what I need to display in the loop item)

• ‘123 My Street’ (the Event Address, which I can now display in the loop item, thanks for your code)

• ‘New York’ (again, I can now display this)

Web guy
Web guy
23 May 2024 7:24 am
Reply to  Web guy

LOL, please ignore my last question. It’s been a very long day, had brain freeze.
Of course, I just use the Post Info widget again then choose the Event Venue taxonomy.