admin管理员组

文章数量:1387342

While I understand that there are many similar questions answered which helped me find a solution to update the page template for my custom post type pragmatically and it works correctly.

However, my difficulty is in finding a solution to update the body_classes that matches the page template. Right now, the body classes are not updated when the page template is updated pragmatically.

add_filter( 'template_include', 'my_template_include', 99, 1 );
function my_template_include( $template ) {
    global $post;

    $meta = get_post_meta( $post->ID, 'designblocks-product-template', true );

    if ( ! $meta ) {
        return $template;
    }

    $get_template = get_post_meta( $meta, '_wp_page_template', true );

    if ( $get_template ) {
        $template = locate_template( array( $get_template ) );
    }

    return $template;
}

Any ideas?

While I understand that there are many similar questions answered which helped me find a solution to update the page template for my custom post type pragmatically and it works correctly.

However, my difficulty is in finding a solution to update the body_classes that matches the page template. Right now, the body classes are not updated when the page template is updated pragmatically.

add_filter( 'template_include', 'my_template_include', 99, 1 );
function my_template_include( $template ) {
    global $post;

    $meta = get_post_meta( $post->ID, 'designblocks-product-template', true );

    if ( ! $meta ) {
        return $template;
    }

    $get_template = get_post_meta( $meta, '_wp_page_template', true );

    if ( $get_template ) {
        $template = locate_template( array( $get_template ) );
    }

    return $template;
}

Any ideas?

Share Improve this question asked Apr 15, 2020 at 12:28 HarisHaris 2312 silver badges5 bronze badges 1
  • Body classes, right? Did you use your own header.php? You may add the same mechanism to your <body> in header.php for outputting the classes. eg. <body <?php body_class( trim( $body_classes ) ); ?>> where you could prepare your own $body_classes. – 西門 正 Code Guy - JingCodeGuy Commented Apr 15, 2020 at 13:03
Add a comment  | 

1 Answer 1

Reset to default 0

Use the body_class filter to change the <body> CSS classes.

You can copy the same logic into your hooked function (like below), or make it neater by copying the logic into another new function and make this function return a boolean (and then use that new function with the body_class filter and within your template_redirect filter.

add_filter( 'body_class', function($body_classes){
    global $post;

    $meta = get_post_meta( $post->ID, 'designblocks-product-template', true );

    if ( ! $meta ) {
        return $body_classes;
    }

    $get_template = get_post_meta( $meta, '_wp_page_template', true );

    if ( $get_template ) {
        $body_classes[] = 'your-custom-classes';
    }

    return $body_classes;
});

Though, if I understood correctly what it is you're trying to do, then you're making your life unnecessarily difficult trying to replicate WP's native page template functionality and related things. Instead, just create a file in your theme called single-{post-type-name}.php and use this instead of assigning a page template. The <body> will be given a class of single-{post-type-name}.

本文标签: Updating page template pragmatically