[Resolved] Add Post Tags to Body Class

Home Forums Support Add Post Tags to Body Class

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #322270
    Sam

    Seen this successfully done with categories, but for some reason I can’t make it work with post tags.

    Usage case: Want all posts with tag “course” to have a specific layout and styling applied. Best way I can think of to target them is to target body.tag-XXXX in CSS.

    This is the code I’ve tried, which adds the tags to the article (same happens with generate_main_class), but not to the body.

      function tags_to_body_class( $classes ) {
        global $post;
        $posttags = get_the_tags( $post->ID );
    
        if ( $posttags ){
            foreach( $posttags as $tag )
                $tags[] = $tag->slug;
            return $tags;
    	}
    	add_filter('body_class', 'tags_to_body_class');
    }
    GeneratePress 1.3.46
    GP Premium 1.3
    #322271
    Sam

    Needed to move add_filter line *OUTSIDE* of the function brackets, duh. Couldn’t find a way to delete this post. Hopefully this helps someone else.

    #322347
    Tom
    Lead Developer
    Lead Developer

    Glad you found a solution! 🙂

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.