admin管理员组文章数量:1130349
There are certain posts where I want to manually append a custom body class using custom fields.
How do I go about it appending the class to the body tag when a certain custom field is added to a post?
I have tried the following but the custom fields name tagbody is not shown in the dropdown:
add_filter( 'body_tag', 'body_tag_name' );
add_filter( 'get_the_body_tag_name', 'body_tag_name' );
function body_tag_name( $name ) {
global $post;
$btag = get_post_meta( $post->ID, 'tagbody', true );
if ( $btag )
$name = $btag;
return $name;
}
There are certain posts where I want to manually append a custom body class using custom fields.
How do I go about it appending the class to the body tag when a certain custom field is added to a post?
I have tried the following but the custom fields name tagbody is not shown in the dropdown:
add_filter( 'body_tag', 'body_tag_name' );
add_filter( 'get_the_body_tag_name', 'body_tag_name' );
function body_tag_name( $name ) {
global $post;
$btag = get_post_meta( $post->ID, 'tagbody', true );
if ( $btag )
$name = $btag;
return $name;
}
Share
Improve this question
edited Dec 11, 2018 at 11:55
AndrewL64
asked Aug 5, 2015 at 19:11
AndrewL64AndrewL64
2034 silver badges18 bronze badges
1 Answer
Reset to default 1You want to use body_class filter.
function prefix_add_body_class( $classes ) {
global $post;
// good to check
if( ! is_single() || 'post' !== get_post_type() ) {
return $classes;
}
$btag = get_post_meta( $post->ID, 'tagbody', true );
if ( empty( $btag ) ) {
return $classes;
}
$classes[] = $btag;
return $classes;
}
add_filter( 'body_class', 'prefix_add_body_class' )
EDIT:
You wrote me in the comments that the theme is your own theme. So you don't need to do it via a filter. Just do it right in your theme.
Edit the header.php file like this.
// some code above
$classes = array();
if( is_single() && 'post' === get_post_type() ) {
$btag = get_post_meta( $post->ID, 'tagbody', true );
if( ! empty( $btag ) ) {
$classes[] = $btag;
}
}
?>
<body <?php body_class( $classes ); ?>>
<?php
// some code below
There are certain posts where I want to manually append a custom body class using custom fields.
How do I go about it appending the class to the body tag when a certain custom field is added to a post?
I have tried the following but the custom fields name tagbody is not shown in the dropdown:
add_filter( 'body_tag', 'body_tag_name' );
add_filter( 'get_the_body_tag_name', 'body_tag_name' );
function body_tag_name( $name ) {
global $post;
$btag = get_post_meta( $post->ID, 'tagbody', true );
if ( $btag )
$name = $btag;
return $name;
}
There are certain posts where I want to manually append a custom body class using custom fields.
How do I go about it appending the class to the body tag when a certain custom field is added to a post?
I have tried the following but the custom fields name tagbody is not shown in the dropdown:
add_filter( 'body_tag', 'body_tag_name' );
add_filter( 'get_the_body_tag_name', 'body_tag_name' );
function body_tag_name( $name ) {
global $post;
$btag = get_post_meta( $post->ID, 'tagbody', true );
if ( $btag )
$name = $btag;
return $name;
}
Share
Improve this question
edited Dec 11, 2018 at 11:55
AndrewL64
asked Aug 5, 2015 at 19:11
AndrewL64AndrewL64
2034 silver badges18 bronze badges
1 Answer
Reset to default 1You want to use body_class filter.
function prefix_add_body_class( $classes ) {
global $post;
// good to check
if( ! is_single() || 'post' !== get_post_type() ) {
return $classes;
}
$btag = get_post_meta( $post->ID, 'tagbody', true );
if ( empty( $btag ) ) {
return $classes;
}
$classes[] = $btag;
return $classes;
}
add_filter( 'body_class', 'prefix_add_body_class' )
EDIT:
You wrote me in the comments that the theme is your own theme. So you don't need to do it via a filter. Just do it right in your theme.
Edit the header.php file like this.
// some code above
$classes = array();
if( is_single() && 'post' === get_post_type() ) {
$btag = get_post_meta( $post->ID, 'tagbody', true );
if( ! empty( $btag ) ) {
$classes[] = $btag;
}
}
?>
<body <?php body_class( $classes ); ?>>
<?php
// some code below
本文标签: phpAdd a custom class to the body tag using custom fields
版权声明:本文标题:php - Add a custom class to the body tag using custom fields 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749109086a2317070.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论