[Resolved] Change header background for category or pages

Home Forums Support [Resolved] Change header background for category or pages

Home Forums Support Change header background for category or pages

Viewing 8 posts - 16 through 23 (of 23 total)
  • Author
    Posts
  • #120906
    anamoore

    Thank you, it worked!

    #120945
    Tom
    Lead Developer
    Lead Developer

    Glad I could help 🙂

    #1459617
    Nitaai Kumar

    This code works perfectly but it also displays the name of the categories on the top of the post in the front end which is not needed at all. How to modify this code so that it just adds the category name to the body class on single posts but does not display the name of the categories in the front end?

    #1460013
    David
    Staff
    Customer Support

    Try this snippet:

    add_filter('body_class','add_category_to_single');
      function add_category_to_single($classes) {
        if (is_single() ) {
          global $post;
          foreach((get_the_category($post->ID)) as $category) {
            // add category slug to the $classes array
            $classes[] = 'category-' . $category->category_nicename;
          }
        }
        // return the $classes array
        return $classes;
    }
    #1659982
    Erik

    Hi! I tried the snippet with adding categories to single post body classes. Works great. Would it be possible also to do this with tags?

    Let’s say I would like to give different styles of the mobile head (with css) on single posts based on a tag. All posts with ‘Orange’ tag gives them orange head when viewing them as single posts. ‘Yellow’ tag gives them yellow head etc.

    And if possible I would like to use the same css class on an archive page or the blog page.

    #1660176
    David
    Staff
    Customer Support

    Hi there,

    for Tags you would use this:

    add_filter( 'body_class', 'add_tags_to_body_class' );
    function add_tags_to_body_class( $classes ) {
    
        if( is_single() && $tags = get_the_tags( get_queried_object_id() ) ) {
            foreach( $tags as $tag ) {
                $classes[] = 'tag-' . $tag->name;
            }
        }
        return $classes;
    }

    Archive pages automatically add the tog-term class to the body.
    But the blog itself does not – so i am not sure what you require there.

    #1660250
    Erik

    Hi David.
    Thanks. Works perfect.

    #1660564
    David
    Staff
    Customer Support

    You’re welcome

Viewing 8 posts - 16 through 23 (of 23 total)
  • You must be logged in to reply to this topic.