$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Add a custom class to the body tag using custom fields|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Add a custom class to the body tag using custom fields

matteradmin10PV0评论

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
Add a comment  | 

1 Answer 1

Reset to default 1

You 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
Post a comment

comment list (0)

  1. No comments so far