Link to home
Start Free TrialLog in
Avatar of igloobob
igloobobFlag for United Kingdom of Great Britain and Northern Ireland

asked on

wordpress if / else for custom fields

Hello, I have a set of custom fields which are not always populated. I need to display them if populated and either display nothing or display a message if they are not.

This is the code to display the fields on the page.

 <h6><span>By: </span><?php yyy_authors(); ?>. <span>Updated on:</span> <?php yyy_date_updated(); ?>. <span>Document type:</span> <?php the_field('document_type'); ?></h6>

Open in new window


So for the sake of figuring this out I'd like it to display the word 'unkown' if any of the fields are empty.

I know I need to be using an if / else but not sure how so can anyone please point me in the direction?

Thanks!
Avatar of Hube02
Hube02
Flag of United States of America image

You're using ACF? :)
I'm assuming so because of the call to the function the_field(), the_field() returns false if no value is set.

$document_type = get_field('document_type');
if  ($document_type) {
  echo $document_type;
} else {
  echo 'unknown';
}

Open in new window

Avatar of igloobob

ASKER

Hi it's actually using ACF for one of the fields but a different plugin for the other two (it's a private plugin pulling documents from external sources that I was supplied with as part of the job).

<?php yyy_authors(); ?>
<?php yyy_date_updated(); ?>

are the ones from the other plugin.

I tried your code above but it just returns 'unknown'
ASKER CERTIFIED SOLUTION
Avatar of Hube02
Hube02
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hi sorry for the delay, I've been on other jobs.

I spoke to the developer and he set it up to include get functions for the fields, it's a custom plugin that's still in development.

Thanks for the advice on that.