0

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?

  • 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. – simongcc Apr 15 at 13:03
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}.

| improve this answer | |
  • You're right. I am trying to automate the body_classes instead of manually adding them. This is a plugin I am trying to build and what I am trying to do add support for page templates in custom CPT. – Haris Apr 15 at 17:34
  • I don't want to add classes manually as they differ from theme to theme. – Haris Apr 15 at 17:34
  • @Haris do you mean you want the class to be named something that is specific to the template? If yes, you could still use the body_class filter and then make the CSS class name something that is unique to that template, e.g. my-template-name-class (this name you could create based on the _wp_page_template meta value). – Dan. Apr 15 at 17:58

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.