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

Need help with PHP if statement WordPress

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'

I posted this here before, and a couple people told me to 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; ?>


I thought the above worked, but it's actually not displaying the icon for anything now, even the fields that do have .pdf in them.

Any ideas?

Answers (4)

2015-09-13

Hariprasad Vijayan answers:

Hello,

Probably that code should work.

Can you make sure get_field('oil_pdf') is not empty?

And also make sure get_field('oil_pdf') returning a string. Please try to echo get_field('oil_pdf') and share the result here.

Regards,
Hariprasad

2015-09-13

Arnav Joy answers:

Hi ,
Can you show me screenshot of admin where this field is ?

2015-09-13

dimadin answers:

Are you using get_field(); inside a loop where it can access global post ID for right post? As others have said, please show example of some of its values, using var_dump() also so we can see type of value.

2015-09-13

S├ębastien | French WordpressDesigner answers:

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

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


and tell me what is returned

Maybe get_field('oil_pdf') return an array




Or : have you seen that : [[LINK href="http://support.advancedcustomfields.com/forums/topic/get_fields-breaking-after-upgrade-to-acf-pro-on-repeater-field/"]]http://support.advancedcustomfields.com/forums/topic/get_fields-breaking-after-upgrade-to-acf-pro-on-repeater-field/[[/LINK]]