Ask your WordPress questions! Pay money and get answers fast! (more info)

ACF if statement WordPress

  • SOLVED

Hello I am using the below code to show Advanced Custom Fields from a post type on my page.

But I would like to add an if statement, so if the custom field is used show the following code, otherwise show nothing.

<a href="<?php echo do_shortcode('[acf field="eps_logo_rgb" post_id="'.get_the_ID().'"]'); ?>" class="nofancybox" download><img src="<?php echo do_shortcode('[acf field="eps_logo_rgb" post_id="'.get_the_ID().'"]'); ?>" width="350" alt="RGB .eps"/></a>
<p><a href="<?php echo do_shortcode('[acf field="eps_logo_rgb" post_id="'.get_the_ID().'"]'); ?>" class="nofancybox" download>Click here to download the RGB .eps file</a></p>


I have this code but do not know how to edit it to add my code:

?php

if(get_field('field_name'))
{
echo '<p>' . get_field('field_name') . '</p>';
}

?>



Thanks

Answers (2)

2014-08-01

Arnav Joy answers:

try this

<?php
if(get_field('field_name', $post_id))

{
?>

<a href="<?php echo do_shortcode('[acf field="eps_logo_rgb" post_id="'.get_the_ID().'"]'); ?>" class="nofancybox" download><img src="<?php echo do_shortcode('[acf field="eps_logo_rgb" post_id="'.get_the_ID().'"]'); ?>" width="350" alt="RGB .eps"/></a>

<p><a href="<?php echo do_shortcode('[acf field="eps_logo_rgb" post_id="'.get_the_ID().'"]'); ?>" class="nofancybox" download>Click here to download the RGB .eps file</a></p>

<?php } ?>


Arnav Joy comments:

where $post_id is the id of the post to which you have to test.


Arnav Joy comments:

please try this code , it will work for you


<?php
if(get_field('eps_logo_rgb', get_the_ID()))

{
?>

<a href="<?php echo do_shortcode('[acf field="eps_logo_rgb" post_id="'.get_the_ID().'"]'); ?>" class="nofancybox" download><img src="<?php echo do_shortcode('[acf field="eps_logo_rgb" post_id="'.get_the_ID().'"]'); ?>" width="350" alt="RGB .eps"/></a>

<p><a href="<?php echo do_shortcode('[acf field="eps_logo_rgb" post_id="'.get_the_ID().'"]'); ?>" class="nofancybox" download>Click here to download the RGB .eps file</a></p>

<?php } ?>

2014-08-01

Hariprasad Vijayan answers:

Try this


<?php
if(get_field('eps_logo_rgb'))
{
?>
<a href="<?php echo do_shortcode('[acf field="eps_logo_rgb" post_id="'.get_the_ID().'"]'); ?>" class="nofancybox" download><img src="<?php echo do_shortcode('[acf field="eps_logo_rgb" post_id="'.get_the_ID().'"]'); ?>" width="350" alt="RGB .eps"/></a>
<p><a href="<?php echo do_shortcode('[acf field="eps_logo_rgb" post_id="'.get_the_ID().'"]'); ?>" class="nofancybox" download>Click here to download the RGB .eps file</a></p>
<?php
}
?>