Solved

Display Custom Field LAbel only if not empty

Posted on 2012-04-07
3
381 Views
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.
0
Comment
Question by:jonathan1978
  • 2
3 Comments
 
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).
<?php
ob_start();
$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
0
 

Author Comment

by:jonathan1978
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.
0
 
LVL 108

Accepted Solution

by:
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…

912 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

19 Experts available now in Live!

Get 1:1 Help Now