[Support request] Single post content

Home Forums Support [Support request] Single post content

Home Forums Support Single post content

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #755395
    Janus

    Hi there

    I need to customize the single post layout for posts in a specific post category.

    What is the best easiest way to do this?

    Thanks

    #755481
    David
    Staff
    Customer Support

    Hi there,

    you can add this PHP 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_nicename;
            }
        }
        // return the $classes array
        return $classes;
    }

    https://docs.generatepress.com/article/adding-php/

    this will add the category slug to the single post body. You can then target it with CSS like so (add appropriate category slug):

    .single-post.category-slug {
    CSS Rules here
    }
    #756243
    Janus

    Yes, I have actually done that, but I would like to (need to) change the markup structure in “content-single.php” in my child theme?

    #756654
    David
    Staff
    Customer Support

    So you would need to have a separate template for that particular category.
    This stack exchange provides options – the responder to the OP gives the method using a filter:

    https://wordpress.stackexchange.com/questions/169831/custom-single-template-for-a-specific-category

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