0

I have running site with many categories, I don't want to mess around with code... I am thinking to add a condition to make -for example- the post title as red if the post has a tag "football", and yellow if the post has tag "baseball". Possible?

  • is your theme using post_class() within the html surrounding the post output in the loop for each post? codex.wordpress.org/Function_Reference/post_class - this would output a tag specific CSS class to the post which you can use for formatting the post title. details depend on your theme and you might want to ask the theme's developer for help with that. – Michael Oct 1 '18 at 0:26
1

I'm not aware of any way to do this without editing code.

I wrote a PHP function which will wrap your post titles in a with the class "hastag-{tag-slug}". You can put this in your theme's functions.php file and then use the_title_with_tags() instead of the_title() in your theme templates.

function the_title_with_tags() {
    //get all tags on this post
    $tags = get_the_tags();
    if ($tags) {
        //store tag slugs in an array
        $classlist = array();
        foreach($tags as $tag) {
            $classlist[] = 'hastag-'.$tag->slug;
        }
        //print the title with tags as CSS classes
        the_title( '<span class="'. implode($classlist, ' ') .'">' , '</span>' );
    }
    else {
        the_title();
    }
}

And the you can add the styling rules to your CSS:

.hastag-football {
    color: red;
}
.hastag-baseball {
    color: yellow;

I think that's cleaner than inline styling.

  • Thank you, but i couldn't apply that in my theme. :) – Bassim Oct 2 '18 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.