php 如何在 Wordpress 中检查 body_class() 中的类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15033888/
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
How to Check for Class in body_class() in Wordpress
提问by Yasir
Within Wordpress header.php, I have
在 Wordpress header.php 中,我有
<body <?php body_class($class); ?>>
How do check to see if a specific class exists, and then load markup as a result? For ex.
如何检查特定类是否存在,然后加载标记作为结果?例如。
<body class"home logged-in">
<?php if $class == 'home' ?>
<div class="home"></div>
<? else : ?>
<div class="internal-page"></div>
<? endif; ?>
Thanks!
谢谢!
回答by s_ha_dum
If you really, really need to use different markup based on the body_classclasses, then use get_body_class
如果你真的,真的需要基于body_class类使用不同的标记,那么使用get_body_class
$classes = get_body_class();
if (in_array('home',$classes)) {
// your markup
} else {
// some other markup
}
But there are probably better ways to do this, like @Rob's suggestion of Conditional Tags. Those map pretty closely to the classes used by body_class.
但是可能有更好的方法来做到这一点,比如@Rob 对Conditional Tags的建议。这些映射非常接近body_class.
回答by Rob
You can access body_classwith a filter add_filter('body_class', function ...)however, I think you are taking the wrong approach. Why not just use css for what you need? For example, .home>div { /* home styles */ }
您可以使用body_class过滤器进行访问,add_filter('body_class', function ...)但是我认为您采用了错误的方法。为什么不只使用 css 来满足您的需要?例如,.home>div { /* home styles */ }
Or you can load a different stylesheet
或者您可以加载不同的样式表
add_filter('body_class', function($classes) {
if (in_array('home', $classes)) {
wp_enqueue_style('home');
}
return $classes;
});
回答by László Péter Varga
I have had the same problem as I created pages using different templates but a custom sub-menu needed to be the same on each pages.
我在使用不同模板创建页面时遇到了同样的问题,但每个页面上的自定义子菜单需要相同。
I tried this one first what is failed
我首先尝试了这个失败的
<body <?php body_class( 'extra-class' ); ?>>
<body <?php body_class( 'extra-class' ); ?>>
The extra classes was added to the body tag but when I run the error log then it was not in the classes array. So I was sure it was added later to the body tag.
额外的类被添加到 body 标记中,但是当我运行错误日志时,它不在类数组中。所以我确定它后来被添加到 body 标签中。
This solution worked for me:
这个解决方案对我有用:
functions.php
functions.php
$GLOBALS['extraBodyClass'] = '';
$GLOBALS['extraBodyClass'] = '';
In the template file
在模板文件中
<?php $GLOBALS['extraBodyClass'] = 'extra-class' ?>- very first line in the template file
<?php $GLOBALS['extraBodyClass'] = 'extra-class' ?>- 模板文件中的第一行
<body <?php body_class( $GLOBALS['extraBodyClass'] ); ?>>- after the template name declaration
<body <?php body_class( $GLOBALS['extraBodyClass'] ); ?>>- 在模板名称声明之后
In the header.phpfile
在header.php文件中
$classes = get_body_class();
if($GLOBALS['extraBodyClass']){
$classes[] = $GLOBALS['extraBodyClass'];
}

