Display Custom Field LAbel only if not empty

Posted on 2012-04-07
Medium Priority
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:Jon Imms
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 111

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

by:Jon Imms
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 111

Accepted Solution

Ray Paseur earned 2000 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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to implement server side field validation and display customized error messages to the client.
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

752 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