Solved

Display Custom Field LAbel only if not empty

Posted on 2012-04-07
3
386 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:Jon Imms
[X]
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
3 Comments
 
LVL 110

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: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.
0
 
LVL 110

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchiā€¦
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

726 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