customization - How to have different background image based on post category

I have below piece of code in style.css file (website : )I am looking for option to have different background-image for

I have below piece of code in style.css file (website : ) I am looking for option to have different background-image for different post/question category.

 .question-content-text {background-image:(../logo);} 

We cannot write php inside css style so could you please help/guide how can this be achieved?

I have below piece of code in style.css file (website : https://askpuzzle) I am looking for option to have different background-image for different post/question category.

 .question-content-text {background-image:(../logo);} 

We cannot write php inside css style so could you please help/guide how can this be achieved?

Share Improve this question asked Apr 19, 2019 at 11:44 Avinash PatelAvinash Patel 398 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You have to have something to target, so that means adding a class with the necessary information for your styles.

I recommend adding categories (or terms from your custom taxonomy) to the body classes. You can do this with a simple filter. This targets single templates and pulls terms from the category taxonomy. You can easily change either of these to suit your needs.

add_filter( 'body_class', 'my_body_class' );
function my_body_class( $classes ) {
    if ( is_single() ) {
        $categories = get_the_terms( get_the_ID(), 'category' );
        if ( $categories ) {
            $category_slugs = array();
            foreach ( $categories as $category ) {
                $category_slugs[] = 'cat-' . $category->slug;
            }
            $classes = array_merge( $classes, $category_slugs );
        }
    }
    return $classes;
}

This will add new classes to your body tag like cat-uncategorized.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745572968a4633793.html

相关推荐

  • customization - How to have different background image based on post category

    I have below piece of code in style.css file (website : )I am looking for option to have different background-image for

    15小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信