Site logo

Archived topic

Change Post Title Colour by Category

1 reply · Started by Shaun on May 28, 2022

Viewing posts 1–2 of 2

Hi
I want to change the title colour of posts with a particular category (category = deals. I want to change it for both single posts and in their WP Show Posts listing.
For example, see the Sample deal at the bottom of the site I have added to private information.
I found the following advice from Leo, which may work for the WP Show Posts list view, but I am unsure.
Note, I only need to change the colour of the deals category of posts. All other post categories use the default settings.
Thanks
Shaun

Hi there,

1. you will need to add this PHP Snippet to your site:

add_filter( 'body_class', function( $classes ) {
    if ( is_single() ) {
        global $post;

        foreach( ( get_the_category( $post->ID ) ) as $category ) {
            $classes[] = 'category-' . $category->category_nicename;
        }
    }

    return $classes;
} );

This will add the category-term class to the body element of the single post.

2. Then you can use this CSS:

.category-deals h1,
.category-deals .wp-show-posts-entry-title a {
   color: #f00 !important;
}

That will target the single post H1 and the WPSP Title link.

This archived topic is closed to new replies.