phpwordpresswordpress-themingelementor

Add class whenever a WordPress category is displayed


On my Wordpress Elementor blog, there are several places where categories are listed. For example below on a blog post page:

<span class="elementor-post-info__terms-list">
    <a href="https://website.nl/culture/" class="elementor-post-info__terms-list-item">Culture</a>
</span>

I'd like to use CSS to style each category with a specific color.

For that, I need to either have the ID added as a class, or display a custom field I have created using ACF. Doesn't matter to me, but I can't figure out how to add a filter/action to add a custom class or a connected ACF paramter whenever a category is displayed on my website.


Solution

  • You can just use a CSS Attribute selector for this:

    a[href*="culture"] {
        color: purple;
    }
    

    [href*="culture"] - selects an a tag where the href attribute contains "culture"

    Since you'll know the term name, you can just build out your CSS for each one. No reason to add a class or hack the term list Elementor widget.

    a[href*="culture"] {
      color: purple;
      font-size: 20px;
    }
    
    a[href*="another"] {
      color: blue;
      font-size: 14px;
    }
    <span class="elementor-post-info__terms-list">
        <a href="https://website.nl/culture/" class="elementor-post-info__terms-list-item">Culture</a>
        <a href="https://website.nl/another/" class="elementor-post-info__terms-list-item">Another</a>
    </span>