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

PHP if statement WordPress

  • SOLVED

I have a snippet of code that basically says to show a field if it's populated. It's an Advanced Custom Field text field.

Here's the working code:


<?php if (get_field('oil_pdf')): ?>
<img src="<?php echo esc_url( home_url( '/' ) ); ?>media/oil-icon-pdf.png"></a>
<?php endif; ?>


I need to change the code to only show if the text field (oil_pdf) includes '.pdf' somewhere in the text string. Not just 'pdf' but '.pdf'

Is this possible?

Answers (2)

2015-09-11

Hariprasad Vijayan answers:

Hi,

Try this,


<?php if (get_field('oil_pdf') && strpos(get_field('oil_pdf'),'.pdf') !== false): ?>
<img src="<?php echo esc_url( home_url( '/' ) ); ?>media/oil-icon-pdf.png"></a>
<?php endif; ?>


Kyler Boudreau comments:

Beautiful - worked. Thank you!

2015-09-11

Kyle answers:

Try this

<?php if (strpos(get_field('oil_pdf'),'.pdf') !== false): ?>

<img src="<?php echo esc_url( home_url( '/' ) ); ?>media/oil-icon-pdf.png"></a>

<?php endif; ?>