0

I am using the Custom Fields Plugin and have 2 custom fields assigned to my "products" custom post type.

Field 1. Price

Field 2 Discount Price

So this is my code:

<div class="wrapper">
<p>Price: <?php the_field('price'); ?></p>
</div> 

"Now this is what I am trying to achieve hypothetically"

 <div class="wrapper">
<?php if (the_field('discount'); )is set: ?>
    <p>Discount: <?php the_field('discount'); ?></p>
<?php endif; ?>

<?php else: ?>
    <p>Price: <?php the_field('price'); ?></p>
    </div><!--wrapper-end--> 


    <?php endif; ?>

I don't know if thats the best way to do what im trying to achieve but I am trying to only display a certain custom field and not the other depending on if one of them is set or not.

So this is really the logic thats giving me trouble:

if <?php the_field('discount'); ?> is set:

I have actually tried using :

<?php $values = get_post_custom_values("discount");

  if ( is_array($values) ): ?>

<p>Discount: <?php the_field('discount'); ?></p>

<?php else: ?>

<p>Price: <?php the_field('price'); ?></p>

<?php endif; ?>

but did not seem to work when I had my price field set and not my discount field it just did not display anything. but if I had set a discount it would display.

Anyone have an idea?

UPDATE

So whenever I set my discount it would show, but when I did not have a discount my original price field didn't show. So that told me that there was something wrong with the else statement.

Never mind scratch that I I tried the code now the if statement is not working and only shows the else which is the price

closed as off-topic by Pieter Goosen, fuxia Dec 12 '14 at 12:59

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • Which Custom fields plugin are you using ACF or CFS ? – Digitalchild Nov 20 '14 at 7:15
  • Advanced Custom Fields – Lucas Santos Nov 20 '14 at 7:17
1

You can use get_field from ACF to check if the field is available and according to the documentation how you conditionally check if a field has contents.

<div class="wrapper">
    <?php if (get_field('discount')) { ?>
        <p>Discount: <?php the_field('discount'); ?></p>
    <?php } else {  ?>
        <p>Price: <?php the_field('price'); ?></p>
    <?php } ?>
</div><!--wrapper-end--> 

This should give you what you want.

  • Hmm so actually it did not work with isset and worked with is_array but only if I used your format on writing the if and else statement.. I really do not understand why the way I wrote my if and else statement did not output the else statement. Because if I set a discount it would show but if I take that out then the original price wouldn't show, so that was telling me something was wrong with my else statement. So i tried the way you wrote your format and it worked. Thank you for the help really appreciate it! – Lucas Santos Nov 20 '14 at 7:00
  • aaahhh scratch that apparently I tried changing up the way I write my if statement and now I only get the regular price and no special even with isset – Lucas Santos Nov 20 '14 at 7:15
  • Wow thanks for the update now that work! guess the conditional was wrong? and I needed to use get_field – Lucas Santos Nov 20 '14 at 7:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.