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

标签 php html css wordpress

我在 Wordpress 中构建自定义主题,当 URL 以 ?checklist-view=1 结尾时,我需要向 body 标记添加 CSS 属性

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

我知道我需要使用超全局 $_GET,但是我是 PHP 的新手。

有没有更简单的方法使用 body_class 模板标签(就像我在下面尝试的那样)或者这是否需要更复杂的 PHP

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

<!doctype html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo('charset'); ?>" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=0">
    <title>Process Street <?php wp_title(); ?></title>
    <link rel="profile" href="http://gmpg.org/xfn/11" />
    <link rel="stylesheet" href="<?php echo get_stylesheet_uri(); ?>" type="text/css" media="screen" />
    <link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>" />
    <!--[if lt IE 9]>
    <script src="<?php echo get_stylesheet_uri(); ?>bower_components/html5shiv/dist/html5shiv.js"></script>
    <![endif]-->
    <?php wp_head(); ?>
</head>
<?php
//?checklist-view=1
$is_checklist = $_GET['checklist-view'];
$class= 'fullscreen';
if($is_checklist == '1') {
    echo $class;
}

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

最佳答案

您不应该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); ?>>

关于php - 如何使用 body_class 将 CSS 类添加到 Wordpress 中的 body 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33290867/

相关文章:

php - 将 Excel 流附加到 swiftmailer 消息中?

php - 如何为特定目录设置open_basedir

php - 如何通过网页将参数传递给 PHP 脚本?

javascript - 如何检查 CSS 和 Javascript 是否支持 CSS @layer?

PHP/Apache 错误 :406 Not Acceptable

jquery - 如何将固定元素包含在相对元素中?

javascript - 为什么我不去那个 URL?

javascript - 在 OpenLayers 中,让一层保持在另一层之上。 (Z 轴)

javascript - 移动 CSS 未按预期显示

html - 填充不影响 Internet Explorer 中的子项