[Last Call] Learn how to a build a cloud-first strategyRegister Now


wordpress if / else for custom fields

Posted on 2014-08-28
Medium Priority
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
  • 2
  • 2
LVL 18

Expert Comment

ID: 40291666
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

ID: 40292111
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

Hube02 earned 2000 total points
ID: 40292374
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: http://www.advancedcustomfields.com/resources/

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

ID: 40314854
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.

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
While Plesk offers many potential benefits to website administrators, including compatibility with Windows Server and other leading technologies, the company has also been working to differentiate it from other control panels for content management…
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

830 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