[Support request] Custom layout for post in specific category

Home Forums Support [Support request] Custom layout for post in specific category

Home Forums Support Custom layout for post in specific category

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #684069
    tobymiguel

    Is it possible to create a file like single.php for a post in a specific category?

    Background: I have some posts in a specific category with a highly customized layout where comments will be displayed in a different place etc. Therefore I want to create a completely different single.php and not just content-single.php for posts in that category. Is this possible with single-category-name.php for example?

    GeneratePress 2.1.4
    GP Premium 1.7.2
    #684127
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    This looks like the best solution: https://wordpress.stackexchange.com/a/168763

    For example:

    add_filter( 'single_template', function( $single_template ) {
        if ( in_category( 1 ) ) {
            $file = get_stylesheet_directory() . '/single-cat-1.php';
            if ( file_exists($file) ) {
                return $file;
            }
        }
    
        return $single_template;
    } );

    You’d have to update the 1 to the ID of your category, and single-cat-1.php to the name of your file, which would have to be in your child theme.

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