如果条件为wordpress中的复选框

时间:2013-11-10 14:30:56

标签: php wordpress

我有以下函数文件http://pastebin.ca/2475191

我正在尝试设置if条件以查看是否选中了复选框。可以找到复选框的字段是

$this->settings['slider'] = array(
    'section' => 'general',
    'title'   => __( 'Enable/Disable Slider' ),
    'desc'    => __( 'Please check the checkbox ' ),
    'type'    => 'checkbox',
    'std'     => 1 // Set to 1 to be checked 
);

在我的后端,我可以看到value="1"没有变化,只是在我检查时添加了checkbox="checked"

首先,我试图查看该值是否在index.php

中得到回应
<?php 
 $settings = get_option('mytheme_options');
 $slider = $settings['slider']; // for echoing the value
 var_dump($slider);
 echo $slider;
?>

如果在后端选中了复选框,我会收到string(1) "1" 1的回复,当未选中时,我会收到Notice: Undefined index: slider,然后在新行NULL

之后

当我尝试添加像这样的条件时

<?php 
  $settings = get_option('mytheme_options');
  $slider = $settings['slider'];
  if ($slider = 1) { ?>
    <div.....HTML
  <?php } else {echo "slider disabled";} ?>

如果未选中复选框,则选中此复选框后,我会看到Notice: Undefined index: slider并且滑块保持可见AND我没有错误,滑块仍然可见。

请帮助我,我是wordpress中的一个菜鸟,尽可能多地学习和修复错误。

1 个答案:

答案 0 :(得分:2)

if (isset( $settings['slider'])) { ?>
    <div.....HTML
  <?php } else {echo "slider disabled";} ?>

检查两个值之间的使用

==(比较运算符)或===(标识运算符)=是赋值运算符

表示赋值。

$slider = 1 // it will assign 1 as an integer value to $slider

/*****************************/

$slider =1;
$slider == 1 // check if $slider value is one both 1 (integer) and "1" a string will return true

/*****************************/


$slider =1;
$slider === "1" //  check if $slider value is 1 and its type is string.  here it will return false as value is of $slider is one but type is int not string