Display Custom Field LAbel only if not empty

Posted on 2012-04-07
Last Modified: 2012-04-11
I am using the Custom Content Manager plugin with wordpress,  and have difiend my custom fields.

One of my custom fields, I want to display a <h3> title </h3  along with a link which is inserted in the custom field.

<h3>Some Title</h3>
<?php print_custom_field('audio:to_link', 'Check out my soundcloud page'); ?>

How can i modifiy this so that if their is no entry in the custom field,  the title does not appear on my page,  I only want to display the <h3> Title  if the custom field is used.
Question by:jonathan1978
  • 2
LVL 108

Expert Comment

by:Ray Paseur
ID: 37819145
There may be a better WP-specific answer, but this strategy will probably work.  Determine what WP produces when you call the php print_custom_field() function with no entry in the custom field.  You can use view source to make this determination.  It might produce an empty string or an empty link tag or something like that.  Once you know that, assign it to a variable so you can test it.  Then you can do something like this (assuming that the function produces a NULL string).
$p_c_empty = NULL;
print_custom_field('audio:to_link', 'Check out my soundcloud page');
$p_c_field = ob_get_clean();
if ($p_c_field != $p_c_empty)
    echo "<h3>Some Title</h3>" . PHP_EOL;
    echo $p_c_field;

Open in new window

HTH, ~Ray

Author Comment

ID: 37820462
That did not work sorry.  It is displaying <h3>Some Title</h3> on pages where their ie no link in the custom field.
LVL 108

Accepted Solution

Ray Paseur earned 500 total points
ID: 37821196
Please re-read the response.  I do not have your web site available to me, so you must do a little bit of your own research.  Specifically, you will need to find out what a variable contains:
It might produce an empty string or an empty link tag or something like that.  Once you know that, assign it to a variable so you can test it.
You will need to use var_dump($p_c_field) to determine what you should assign to the variable named $p_c_empty.

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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 viewer will learn how to dynamically set the form action using jQuery.

747 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

11 Experts available now in Live!

Get 1:1 Help Now