Wordpress -在特定页面的主div中添加类



我有一个页面我只需要改变这个页面的背景颜色这个页面的结构是这样开始的

<body class="...">
<div class="start-class">
......
</div>
</body>

,我想把functions.php:

add_filter( 'body_class', 'my_css');
function my_css( $classes ) {
if ( is_page(82) )
find div that has specific "start-class" because it doesn't have any id
$classes[] = 'my-custom';

return $classes; 
}

有办法添加它吗?谢谢大家!!干杯!

如果您更改/添加以下php代码到header.php文件中的body标记…

<body <?php body_class(); ?> >

…, Wordpress会自动添加"正文类",其中有一个page-id-xxx,其中";xxx"表示与数据库中的页id相对应的每个页的单个id号。然后,您可以为该id (#page-id-xxx { background-color: #abc; })创建一个带有background-color的CSS规则,并将其添加到通用样式表或通过自定义器仅添加到该页面。

使用下面的代码为特定页面添加body class

add_filter('body_class', 'custom_body_class');
function custom_body_class($classes) {
if (is_page(38034))
$classes[] = 'new-class';
return $classes;
}

基于自定义类,您可以更改特定页面的背景颜色。也可访问https://developer.wordpress.org/reference/functions/body_class/

最后,我决定在body内部的所有级别的容器元素上应用完全透明:

background-color:rgba(0,0,0,0.0) !important;

在任何适用的地方,所以我可以使用背景内体与add_filter( 'body_class', 'my_css');我在帖子中说,这样让我的生活更容易…无论如何,感谢所有人@Johannes, @Broe, @Rahul Srivastava !干杯! !

最新更新