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 |1 Answer
Reset to default 0Use 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
版权声明:本文标题:Updating page template pragmatically 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744566160a2613060.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
<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