0

I have two different post type categories on the site I'm building at the moment - "press" and "blog". I used the following code, inserted in my child theme's functions.php file, to get the "press" posts to use the "single-press.php" template:

add_filter( 'single_template', function ( $single_template ) {

$parent     = '7'; 
$categories = get_categories( 'child_of=' . $parent );
$cat_names  = wp_list_pluck( $categories, 'name' );

if ( has_category( 'press' ) || has_category( $cat_names ) ) {
    $single_template = dirname( __FILE__ ) . '/single-press.php';
}
return $single_template;

}, PHP_INT_MAX, 2 );

This did work, but the problem I'm having now is that the "blog" posts (which have an id of '6' and should just be using the single.php template) are using this category-specific template as well. I've tried a bunch of different things to correct it but haven't had any luck so far, so I thought I'd post here and see if anyone had any thoughts. My PHP skills are not great, so I apologize in advance if this is just the result of a pretty basic error on my part that I'm not catching. Thanks so much!

0

You don't have to set that code in your functions.php.

Naming a file single-press.php should do the trick if you named well you custom post type when you declared it. The post type should continue to use single.php.

I this doesn't work, please provide wp version and custom post type declaration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.