php 向 Wordpress body 标签添加自定义类名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2466073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:34:39  来源:igfitidea点击:

Add a custom class name to Wordpress body tag?

phpwordpress

提问by Scott B

I'd like to place a directive in my theme's functions.php file which appends a classname to the wordpress body tag. Is there a built-in API method for this?

我想在我的主题的functions.php 文件中放置一个指令,它将类名附加到wordpress body 标签中。是否有内置的 API 方法?

For example, my body tag code is...

例如,我的身体标签代码是...

<body <?php if(function_exists("body_class") && !is_404()){body_class();} else echo 'class="page default"'?>>

And it results in the following being written to the body tag (depending on the context in which the page is presented (page, post, logged-in, etc)

它导致以下内容被写入正文标签(取决于页面呈现的上下文(页面、帖子、登录等)

<body class="home blog logged-in"> 

Depending on the child theme I'm using at the time, I want it to be...

根据我当时使用的儿童主题,我希望它是...

<body class="home blog logged-in mychildthemename"> 

回答by Richard M

You can use the body_classfilter, like so:

您可以使用body_class过滤器,如下所示:

function my_plugin_body_class($classes) {
    $classes[] = 'foo';
    return $classes;
}

add_filter('body_class', 'my_plugin_body_class');

Although, obviously, your theme needs to call the corresponding body_classfunction.

虽然,很明显,你的主题需要调用相应的body_class函数。

回答by mondrey

You can also use it directly inside the WP body_class function which will append the string inside body class.

您也可以直接在 WP body_class 函数中使用它,该函数将在 body 类中附加字符串。

eg.

例如。

$class="custom-class";
<body <?php body_class($class); ?>>

http://codex.wordpress.org/Function_Reference/body_class

http://codex.wordpress.org/Function_Reference/body_class

回答by Tomas Buteler

In case you're trying to add classes to the body tag while in the Admin area, remember to use the admin_body_classhook instead. Note that it's a filter which works slightly different since it passes a string of classes rather than an array, so your code would look like this:

如果您在管理区域中尝试向 body 标记添加类,请记住改用 admin_body_class钩子。请注意,它是一个过滤器,它的工作方式略有不同,因为它传递的是类字符串而不是数组,因此您的代码如下所示:

add_filter('admin_body_class', 'my_admin_body_class');
function my_admin_body_class($classes) {
    return $classes . ' my_class';
}

回答by Arul M joseph

Try this..

尝试这个..

    add_filter( 'body_class', 'custom_class' );
function custom_class( $classes ) {
    if ( !is_front_page() ) {
        $classes[] = 'example';
    }
    return $classes;
}

回答by che

If you just want to add class based on your page template note that WP now automatically adds per-template class to body tag.

如果您只想根据页面模板添加类,请注意 WP 现在会自动将每个模板类添加到 body 标签。

回答by Arif Rahman

To add more classes to the filter, just add another line that adds another value in to the array:

要向过滤器添加更多类,只需添加另一行将另一个值添加到数组中:

add_filter( 'body_class','my_body_classes' ); function my_body_classes( $classes ) {

add_filter('body_class','my_body_classes'); 函数 my_body_classes( $classes ) {

$classes[] = 'class-name';
$classes[] = 'class-name-two';

return $classes;

}

}

回答by code_burgar

Simply edit your theme's header.php and change the class there (manually or according to some preset logic rules).

只需编辑您的主题的 header.php 并更改那里的类(手动或根据一些预设的逻辑规则)。