0

Ok, I think I have a simple fix here. I'm using ACF to link health reports to specific dogs on a breeder website. But, not all dogs have the same reports, and I don't want to show a dead link if no ACF value is available. So, I wanted to use an if statement but can't quite get it to work. Here's my code:

<?php
            $values = get_field('cardiac');
            if($values) {
            echo '<a rel="lightbox" href="'.<?php the_field('cardiac'); ?>.'">Cardiac</a>';
            } else {
            echo '';
            }
        ?>

Any help would be appreciated.

  • 1
    What is var_dump($values);? – s_ha_dum Feb 6 '14 at 18:50
  • I got bool(false) after my test field – mtuttle Feb 6 '14 at 20:33
  • Is this code in a Loop? – s_ha_dum Feb 6 '14 at 20:43
  • Yes, I added it within a loop. – mtuttle Feb 6 '14 at 21:13
2

My guess is that your problem is that your field type is returning some non-empty value, even if it's not a URL. For instance, do you have "http://" set as a default value for that field in the admin? If that were the case you could need to use the following as your test:

if( $values || $values !== 'http://' )...

Alternately, your field may be returning an empty array or some other kind of "unechoable" value.

Try putting var_dump( $values ) after you set the $values variable and see what you're working with. Then adjust your if() statement to account for other possible non-empty values you may want to weed out before outputting a link.


Update: Another issue

Also, you shouldn't use the_field() in an echo statement since the_field already echos its output. Change this:

echo '<a rel="lightbox" href="'.<?php the_field('cardiac'); ?>.'">Cardiac</a>';

To this:

echo '<a rel="lightbox" href="'. $values .'">Cardiac</a>';

  • I added the code above but it's still showing the link on fields with no value. The fields are all set as image url. – mtuttle Feb 6 '14 at 19:28
  • What do you get with the var_dump() on a field that you think shouldn't output a link? – mrwweb Feb 6 '14 at 19:42
  • Was I supposed to see an error output? – mtuttle Feb 6 '14 at 19:57
  • Sorry, I got bool(false) after my test field. – mtuttle Feb 6 '14 at 20:04
  • see update. I don't think it'll fix your issue, but it is wrong. – mrwweb Feb 6 '14 at 21:37
0

I'm using the same type of setup. However, I'm not using the else statement. Here's how mine looks (applied with your variables):

$values = get_field('cardiac');
    if($values) { ?>
        <a href="<?php the_field('cardiac') ;?>" title="<?php echo the_title();?>" data-lightbox="<?php echo the_title();?>">Cardiac</a>
<?php } ;
  • Weird, I just tried your code, but the link still shows on the fields with no value. – mtuttle Feb 6 '14 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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