让我更新您的代码:)
您不需要为您的帖子选择祖父母类别。首先,我们需要获得祖父母类别名称。这里是函数;
function get_grandparents_category_salgur( $id) { 
    $parent = get_term( $id, \'category\' ); 
    if ( is_wp_error( $parent ) ) 
        return $parent; 
     if ( $parent->parent && ( $parent->parent != $parent->term_id ) ) { 
        $go_get_gp = get_term( $parent->parent, \'category\' );
    }
    $grandparent = get_term( $go_get_gp->parent, \'category\' );  
    return $grandparent->name; 
} 
 此代码将查找祖父母类别名称。之后,我们可以在函数中定义。
function get_custom_cat_template($single_template) {
    global $post;
    $postcat = get_the_category( $post->ID );
    $grandparent_name = get_grandparents_category_salgur( $postcat[0]->term_id);
        if ( $grandparent_name === \'Grandparent Category\' ) {
            $single_template = dirname(__FILE__) . \'/single-template.php\';
        }
    return $single_template;
}
add_filter( "single_template", "get_custom_cat_template" );