如何使用body_class将CSS类添加到Wordpress中的body标签

时间:2015-10-22 21:14:21

标签: php html css wordpress

我正在Wordpress中构建一个自定义主题,当URL结束时我需要在body标签中添加一个CSS属性?checklist-view = 1

我的代码有什么问题,因为没有添加全屏?

我知道我需要使用超全局$ _GET,但我完全不熟悉PHP。

是否有一种更简单的方法可以使用body_class模板标记(比如我在下面尝试)或者这需要更复杂的PHP

编辑:将$ class更改为等于字符串,而不是将其设置为数组。

 try took 6906539
null took 3801656
 try took 13380219
null took 87684
 try took 1036815
null took 79558
 try took 1021416
null took 10693
 try took 1020989
null took 1711
null took 1284
null took 1283
null took 1283
null took 1283
null took 1711
 try took 1038954
 try took 1106107
 try took 1040237
 try took 1020134
 try took 1028261

1 个答案:

答案 0 :(得分:1)

你不应该echo这个类,而是应该将它分配给稍后传递给body_class()的变量。我还会检查checklist-view是否已设置,以避免错误:

<?php

$is_checklist = $_GET['checklist-view'];
if ( !empty( $is_checklist ) && $is_checklist === '1') {
    $class= 'fullscreen';
} else {
    $class = '';
}

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

您也可以使用三元运算符实现相同的功能:

$checklist = $_GET['checklist-view'];
$class = ( !empty( $checklist ) && $checklist === '1') ? 'fullscreen' : '';
?>

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