在动态php样式表中添加PHP IF语句

时间:2018-12-13 20:05:56

标签: php css wordpress dynamic

我有一些复选框,当选中时,它们通过Ajax将它们各自的类添加到<body>元素中:

<input type="checkbox" name="<?php echo $opt;?>" class="onoffswitch-checkbox" id="myonoffswitch-<?php echo $n;?>" value="true"<?php echo (dh_switch_setting_get($opt, '0') == '0' ? "" : " checked='checked'");?> />
<label class="onoffswitch-label" for="myonoffswitch-<?php echo $n;?>" data-class="<?php echo $obj['class'];?>"></label> 

(function($) {
    $(function() {
        $("LABEL[for^=myonoffswitch]")
        .each(function() {
            // ---Which label was clicked? ---
            var which = $(this).attr("for");
            var cls = $(this).data("class");
            if ( $("INPUT[id=" + which + "]").is(":checked") ) {
                $("BODY").addClass(cls);
            } else {
                $("BODY").removeClass(cls);
            }

            $(this).parents(".onoffswitch").on("click", function() {
                if (!$("INPUT[id=" + which + "]").is(":checked")) {
                    $("BODY").addClass(cls);
                } else {
                    $("BODY").removeClass(cls);
                }

                var val = $("INPUT[id=" + which + "]").is(":checked") ? "1" : "0";
                console.info("Set " + which + " to " + val)

                $.ajax({
                    url: './',
                    type: "POST",
                    data: $("INPUT[id=" + which + "]").attr("name") + "=" + val + "&<?php echo MD5("DH");?>=1", 
                    success: function (response) {
                    },
                    error: function (jqXhr, e, responseText) {
                        console.log(arguments);
                        alert("Oops. Something went wrong: " + responseText);
                    }
                });
            });
        });
    });
})(jQuery);

我还有一个动态的PHP样式表style.php,如下所示:

<?php
header("Content-type: text/css;charset: UTF-8");
require('styles1.php');
require('styles2.php');
require('styles3.php');
?>

我试图使一些IF语句在正文具有类的地方工作,然后 require()指定的样式。

我尝试了这种变化,但没有任何效果:

<?php
header("Content-type: text/css;charset: UTF-8");
$classes = get_body_class();
if (in_array('class1',$classes)) {
   require ( 'styles1.php' );
}
if (in_array('class2',$classes)) {
   require ( 'styles2.php' );
}
if (in_array('class3',$classes)) {
   require ( 'styles3.php' );
}
?>

有人可以帮我吗?

0 个答案:

没有答案