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>
}
ejntaylor
  • 1,900
  • 1
  • 25
  • 43
  • "field_name" you are using this one as custom field, I mean the name or is it something else, also try dumping the value var_dump ( get_field('field_name') ); ?? – sven Oct 15 '13 at 09:58
  • Thanks. field_name is the custom field. I have set this correctly in my code. Not sure about var_dump - sorry! – ejntaylor Oct 15 '13 at 11:06
  • var_dump is just to check what value you are getting – sven Oct 15 '13 at 11:11
  • doh! I get: bool(false) – ejntaylor Oct 15 '13 at 11:14
  • That just means your field value is not updating when you are saving your data to database. – sven Oct 15 '13 at 11:17
  • Thanks for the help - I've now fixed this and will post the answer in 6 hours time - I'm a new user so can't answer straight away. The var_dump was super useful. – ejntaylor Oct 15 '13 at 11:33

2 Answers2

2

Ok, I re-read the docs for ACF and found the following (http://www.advancedcustomfields.com/resources/how-to/how-to-get-values-from-a-taxonomy-term/)

So I applied with some logic and now it works. Thanks for var_dump pointer as that helped me fix this.

// 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) { ?>
ejntaylor
  • 1,900
  • 1
  • 25
  • 43
  • Is this also for inside loop and just outside the loop ? – Jamil Ahmed Aug 20 '14 at 18:25
  • Should be fine for either as in the second brackets of get_field we set where ACF needs to look. If that second bracket is not included then it will look within the current loop. – ejntaylor Aug 21 '14 at 19:07
0

missing bracket in the if statement , change

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

with

if (is_product_category() && get_field('field_name') == true) )
user2092317
  • 3,226
  • 5
  • 24
  • 35
  • Thanks for the quick feedback. I'm afraid that doesn't work, but it did point out that I had missed a bracket on the if statement above, which I've now fixed/ – ejntaylor Oct 15 '13 at 09:50
  • `if (is_product_category() && get_field('field_name') == true) )` – user2092317 Oct 15 '13 at 09:52
  • Brackets fixed but not solved. I can get the different output from changingg == true or == false for get_field('product_cat_two_col_template') - It seems that get_field('product_cat_two_col_template') is always false. I thought it might be because it is out of the loop. – ejntaylor Oct 15 '13 at 09:58
  • what do you want to do with is_product_category(), are you trying to check if that is set or not ? – user2092317 Oct 15 '13 at 10:01
  • tbh is_product_category() is not relevant, i added it as i wanted to check with multiple params. is_product_category() is working well and is true if on product page. My problem lies with the get_field('field_name') from ACF. This is a true/false selector i have added to the product categories. From within the product category I can set this. I then want to use this within my IF statement, but it is always false. – ejntaylor Oct 15 '13 at 10:08
  • remove `is_product_category() &&` and check if it is true – user2092317 Oct 15 '13 at 10:10