Empty "isset" still shows up on page

Hi all,

Quick question. On my (wordpress-)website, we use a range of custom fields to add additional info to a post. Not all of these fields, in this case the dates (datum2, datum3, datum4, …, datum7) are mandatory, so I only want those that have been filled in to show.

Thing is, it shows ALL the <li>'s, even the ones that have no custom value set for them. For datum7, it shows a list item with only the <br /> tag and the “dag 7” text, while I want no list item at all as “datum7” was left empty.

This is the code:


<?php 
if ( have_posts() ) while ( have_posts() ) : the_post(); 
$locatie = get_post_custom_values("locatie");
$startdatum = get_post_custom_values("startdatum");
$datum2 = get_post_custom_values("datum2");
$datum3 = get_post_custom_values("datum3");
$datum4 = get_post_custom_values("datum4");
$datum5 = get_post_custom_values("datum5");
$datum6 = get_post_custom_values("datum6");
$datum7 = get_post_custom_values("datum7");
$prijs = get_post_custom_values("prijs");
?>
    <h3>cursusdata</h3>
    <ul>
    <li><strong><?php echo $startdatum[0]; ?></strong><br />dag 1</li>
    <?php if(isset($datum2)) { echo '<li><strong>'. $datum2[0] .'</strong><br />dag 2</li>'; } ?>
    <?php if(isset($datum3)) { echo '<li><strong>'. $datum3[0] .'</strong><br />dag 3</li>'; } ?>
    <?php if(isset($datum4)) { echo '<li><strong>'. $datum4[0] .'</strong><br />dag 4</li>'; } ?>
    <?php if(isset($datum6)) { echo '<li><strong>'. $datum5[0] .'</strong><br />dag 5</li>'; } ?>
    <?php if(isset($datum6)) { echo '<li><strong>'. $datum6[0] .'</strong><br />dag 6</li>'; } ?>
    <?php if(isset($datum7)) { echo '<li><strong>'. $datum7[0] .'</strong><br />dag 7</li>'; } ?>
    </ul>
<?php endwhile; wp_reset_query(); ?>

What’s wrong with this code? I’ve used a very similar method before, and it worked fine…

Thanks in advance for your assistance.

Stef

isset() just tests if a variable is set, which it is in your code above - you’re assigning each variable to the returned value of get_post_custom_values(). Depending on what get_post_custom_values() returns you will need to check for false or a blank string instead.

Damn, I figured it would be something like that… How do I check whether one of these values is empty? I’m not that much of a PHP-hero. :frowning:

By the way, I looked up the code I used for another project, and this works fine:


<?php if ( have_posts() ) while ( have_posts() ) : the_post(); 

$bouwer = get_post_custom_values("bouwer");
$link = get_post_custom_values("link");
$telefoon = get_post_custom_values("telefoon");
$adres = get_post_custom_values("adres");
$info_nl = get_post_custom_values("info_nl");
$info_fr = get_post_custom_values("info_fr");

?>

    <h3><?php _e('[:nl]omschrijving[:fr]d&eacute;scription'); ?></h3>
    
    <!-- load project info in correct language if available, trim summary and embed in modal fancybox popup -->
    <?php 
    if (isset($info_nl) && qtrans_getLanguage() == "nl") { 
        $trim_length = 70;
        $custom_field = 'info_nl';
        $value = get_post_meta($post->ID, $custom_field, true);
        if ($value) {
            echo rtrim(substr($value,0,$trim_length));
        }
    echo '... <a class="inline" href="#data">meer lezen</a><div style="display:none"><div id="data">'. $info_nl[0] .'</div></div><br/>'; 
    
    } elseif (isset($info_fr) && qtrans_getLanguage() == "fr") { 
        $trim_length = 70;
        $custom_field = 'info_fr';
        $value = get_post_meta($post->ID, $custom_field, true);
        if ($value) {
            echo rtrim(substr($value,0,$trim_length));
        }
    echo '... <a class="inline" href="#data">lire plus</a><div style="display:none"><div id="data">'. $info_fr[0] .'</div></div><br/>';
    } 
    ?>
<?php endwhile; ?>

god I hate wordpress.

from their “documentation”:

{get_post_custom_values} returns nothing if no such key exists, or none is entered.
So I don’t know what “nothing” is…false? null? empty string? empty array?

You could try replacing this:

<?php if(isset($datum2)) { echo '<li><strong>'. $datum2[0] .'</strong><br />dag 2</li>'; } ?>

with

<?php if(!is_null($datum2)) { echo '<li><strong>'.  $datum2[0] .'</strong><br />dag 2</li>'; } ?>

or

<?php if(!empty($datum2)) { echo '<li><strong>'.  $datum2[0] .'</strong><br />dag 2</li>'; } ?>

or

<?php if(!$datum2) { echo '<li><strong>'.  $datum2[0] .'</strong><br />dag 2</li>'; } ?>

until you find one that works. good luck.

Both functions is_null() and empty() are incorrect. This because empty() will return true for actual values such as; 0,0.00 and the list goes on. That said isset() and is_null() both return false for a null value.

Do a var_dump() on the return values and I will expect WordPress being the pile of trash it is that the method get_post_custom_values() returns an empty string rather then nothing (null).

The wp function in question is supposed to return an array or “nothing”, so empty() will return true for an empty array, empty string, null, false, or 0.

ummm…is_null() returns true when fed a null value :wink:

None of the suggestions above works. The “if(!datum2)” even shows up as a blank space… :frowning:

I also don’t understand why this worked for the other example I provided, and not for this page.

After thinking about this again, there is the possibility that the function literally does not return anything, in which case isset() should work just like you said, but !empty() and !is_null() should also work.

There might be something else going on - I would try a var_dump of each of the variables like oddz suggested, i.e.:

if ( have_posts() ) while ( have_posts() ) : the_post();
$locatie = get_post_custom_values("locatie");
$startdatum = get_post_custom_values("startdatum");
$datum2 = get_post_custom_values("datum2");
$datum3 = get_post_custom_values("datum3");
$datum4 = get_post_custom_values("datum4");
$datum5 = get_post_custom_values("datum5");
$datum6 = get_post_custom_values("datum6");
$datum7 = get_post_custom_values("datum7");
$prijs = get_post_custom_values("prijs");

echo '<pre>';
var_dump($datum2, $datum3, $datum4, $datum5, $datum6, $datum7);
echo '</pre>';


Returns this on the frontend page:

array(1) {
[0]=>
string(14) “3 oktober 2011”
}
array(1) {
[0]=>
string(15) “24 oktober 2011”
}
array(1) {
[0]=>
string(16) “14 november 2011”
}
array(1) {
[0]=>
string(14) “9 januari 2012”
}
array(1) {
[0]=>
string(0) “”
}
array(1) {
[0]=>
string(0) “”
}

And in this scenario you want the last 2 items to not show up, correct?

So by empty it means an array with any empty string , what a f**kin pile of sh*t. Since it seems there is always a single item I would just call array_pop() immediately after that get_post_custom_values() function get the actual values your after. Something like the below than use strlen() === 0 to determine the fields without data.


<?php
if ( have_posts() ) while ( have_posts() ) : the_post();
$locatie = array_pop(get_post_custom_values("locatie"));
$startdatum = array_pop(get_post_custom_values("startdatum"));
$datum2 = array_pop(get_post_custom_values("datum2"));
$datum3 = array_pop(get_post_custom_values("datum3"));
$datum4 = array_pop(get_post_custom_values("datum4"));
$datum5 = array_pop(get_post_custom_values("datum5"));
$datum6 = array_pop(get_post_custom_values("datum6"));
$datum7 = array_pop(get_post_custom_values("datum7"));
$prijs = array_pop(get_post_custom_values("prijs"));
?>
    <h3>cursusdata</h3>
    <ul>
    <li><strong><?php echo $startdatum; ?></strong><br />dag 1</li>
    <?php if(strlen($datum2) !== 0) { echo '<li><strong>'. $datum2 .'</strong><br />dag 2</li>'; } ?>
    <?php if(strlen($datum3) !== 0) { echo '<li><strong>'. $datum3 .'</strong><br />dag 3</li>'; } ?>
    <?php if(strlen($datum4) !== 0) { echo '<li><strong>'. $datum4 .'</strong><br />dag 4</li>'; } ?>
    <?php if(strlen($datum6) !== 0) { echo '<li><strong>'. $datum5 .'</strong><br />dag 5</li>'; } ?>
    <?php if(strlen($datum6) !== 0) { echo '<li><strong>'. $datum6 .'</strong><br />dag 6</li>'; } ?>
    <?php if(strlen($datum7) !== 0) { echo '<li><strong>'. $datum7 .'</strong><br />dag 7</li>'; } ?>
    </ul>
<?php endwhile; wp_reset_query(); ?>

quick and dirty – the wp motto!

Yep :rofl:

@ThaVincy - You could perhaps create a custom function to handle this - the following should be bullet-proof since that wp function seems to return god-knows-what:

<?php
function doDisplay($post_custom_values_array)
{
    return    isset($post_custom_values_array) &&
            is_array($post_custom_values_array) &&
            array_key_exists(0, $post_custom_values_array) &&
            $post_custom_values_array[0] != '';
}
?>

then change your list items like so:

<?php if(doDisplay($dateum2)) { echo '<li><strong>'.  $datum2[0] .'</strong><br />dag 2</li>'; }  ?>