我的PHP条件逻辑是不正确的还是我做了一些愚蠢的错误

时间:2017-05-24 17:44:18

标签: php wordpress

这个逻辑是一个PHP逻辑,但是一个用于发布元数据的wordpress函数→

 <?php
               // U need to use this to set the checked="checked"
               $checkbox_value = get_post_meta($object->ID, "meta-box-radio", true);
           ?>
                      <label>
                          <input type="radio" name="meta-box-radio" value="sidebar-rhs"<?php if($checkbox_value == 'sidebar-rhs'){echo 'checked =\"checked\"';} ?> /><img src="<?php echo get_template_directory_uri(); ?>/inc/admin/img/sidebar-lhs.png" width="10%" height:auto;>
                      </label>

                      <label>
                          <input type="radio" name="meta-box-radio" value="sidebar-lhs" <?php if($checkbox_value == 'sidebar-lhs'){echo 'checked =\"checked\"';} ?>/><img src="<?php echo get_template_directory_uri(); ?>/inc/admin/img/sidebar-rhs.png" width="10%" height:auto;>
                      </label>

                      <label>
                          <input type="radio" name="meta-box-radio" value="sidebar-none" <?php if($checkbox_value == 'sidebar-none'){echo 'checked =\"checked\"';} ?>/><img src="<?php echo  get_template_directory_uri(); ?>/inc/admin/img/sidebar-none.png" width="10%" height:auto;>
                      </label>
       </div>

现在这个逻辑的作用实际上有助于选择Post模板。

我正在使用上面的逻辑,其中一个应该打印一个额外的类。 This Video will show该课程没有回应。

我正在做这个→

<aside class="main-sidebar col <?php if($checkbox_value == 'sidebar-lhs') {echo 'main-sidebar-reverse'; } ?> ">
    <?php dynamic_sidebar( 'sidebar1' ); ?>
</aside>

但是,尽管选择了左侧边栏并成功保存在Wordpress中,但此类→main-sidebar-reverse未打印在HTML中。

  

这是一个WORDPRESS问题,但是这是一个纯粹的PHP相关的问题,为什么我在这里发帖。

1 个答案:

答案 0 :(得分:1)

您正尝试在不同的文件中使用变量。对于大多数情况,它将超出范围,不会在另一个文件中定义。

您可以尝试再次调用该函数:

<aside class="main-sidebar col <?php if(get_post_meta($object->ID, "meta-box-radio", true) == 'sidebar-lhs') {echo 'main-sidebar-reverse'; } ?> ">
    <?php dynamic_sidebar( 'sidebar1' ); ?>
</aside>

或者如果您计划在此文件中多次使用变量的内容:

<?php
$checkbox_value = get_post_meta($object->ID, "meta-box-radio", true);
?>
<aside class="main-sidebar col <?php if($checkbox_value == 'sidebar-lhs') {echo 'main-sidebar-reverse'; } ?> ">
    <?php dynamic_sidebar( 'sidebar1' ); ?>
</aside>