wordpress if / else for custom fields

Posted on 2014-08-28
Last Modified: 2014-09-10
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?

Question by:igloobob
    LVL 18

    Expert Comment

    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


    Author Comment

    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'
    LVL 18

    Accepted Solution

    Unfortunately, I don't know what that other plugin is by looking at those two functions, I have never encountered them before.

    ACF is completely documented here:

    For example, i swapped the function you were using "the_field()" that outputs the value stored in the field with the function "get_field()" that returns the value so that you can use/test it before display.

    It appears that these other two functions from the other plugin are also display functions. You need to find out if they have equivalent "get" type functions, use those functions and then test your values before displaying them. Your best hope at finding out what to replace those functions with is the plugin documentation, if there is any.

    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    In Part I (, I introduced you to the powerful WordPress backend, the WordPress administrative Dashboard.  In Part II, I will introduce yo…
    The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
    The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now