4

I have added a true/false checkbox using Advanced Custom Fields for Wordpress. I want to be able to select an option which amends the page template.

I am adding this option to the Product Category in WooCommerce / Wordpress. I have included this bit of logic in the code.

I have the following code but it does not work. I suspect it is because it is not within the loop. However the code I want to insert includes the loop. Any ideas/guidance on the code is much appreciated

<?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>
}
4

2 回答 2

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) { ?>
于 2013-10-16T10:23:51.210 回答
0

if 语句中缺少括号,请更改

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

if (is_product_category() && get_field('field_name') == true) )
于 2013-10-15T09:44:34.040 回答