向 Wordpress body 标签添加自定义类名? [英] Add a custom class name to Wordpress body tag?
问题描述
我想在我的主题的functions.php 文件中放置一个指令,该指令将一个类名附加到wordpress body 标签中.是否有内置的 API 方法?
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?
例如,我的 body 标签代码是...
For example, my body tag code is...
<body <?php if(function_exists("body_class") && !is_404()){body_class();} else echo 'class="page default"'?>>
它会导致以下内容被写入 body 标签(取决于页面呈现的上下文(页面、帖子、登录等)
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">
推荐答案
您可以使用 body_class
过滤器,如下所示:
You can use the body_class
filter, like so:
function my_plugin_body_class($classes) {
$classes[] = 'foo';
return $classes;
}
add_filter('body_class', 'my_plugin_body_class');
虽然,显然,您的主题需要调用相应的body_class
功能.
Although, obviously, your theme needs to call the corresponding body_class
function.
这篇关于向 Wordpress body 标签添加自定义类名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!