高级自定义字段 - 如果/ Else语句带有true / false复选框

时间:2013-10-15 09:41:20

标签: php wordpress wordpress-theming woocommerce advanced-custom-fields

我已使用Wordpress的高级自定义字段添加了一个真/假复选框。我希望能够选择一个修改页面模板的选项。

我将此选项添加到WooCommerce / Wordpress中的产品类别。我在代码中包含了这一点逻辑。

我有以下代码,但它不起作用。我怀疑是因为它不在循环内。但是我要插入的代码包括循环。关于代码的任何想法/指导都非常感谢

<?php if (is_product_category() && get_field('field_name') == true) { ?>

    <div class="custom-sidebar-right">
            <?php if ( have_posts() ) : ?>
            <?php while ( have_posts() ) : the_post(); ?>
                <?php woocommerce_get_template_part( 'content', 'product' ); ?>                 
            <?php endwhile; // end of the loop. ?>

    </div>

<?php } elseif (is_product_category() && get_field('field_name') == false ) { // Added close brace

<div> Empty Test </div>
}

2 个答案:

答案 0 :(得分:2)

好的,我重新阅读了ACF的文档并找到了以下内容(http://www.advancedcustomfields.com/resources/how-to/how-to-get-values-from-a-taxonomy-term/

所以我应用了一些逻辑,现在它可以工作了。感谢var_dump指针,因为这有助于我解决这个问题。

// vars
$queried_object = get_queried_object(); 
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;  

$is_field_name = get_field('field_name', $taxonomy . '_' . $term_id);

if (is_product_category() && $is_field_name == false) { ?>

答案 1 :(得分:0)

在if语句中缺少括号, 改变

  if (is_product_category() && get_field('field_name') == true) 

if (is_product_category() && get_field('field_name') == true) )
相关问题