• Hi,

    I am using custom fields for my posts. For example, I might have a key “Mood” with a value “Happy”. However, I might not want to add my “Mood” field to every post. What code could I use to hide the field from the post if the value for that key is not present? In other words, if I don’t enter anything for a value for the “Mood” key in the custom fields, how can I hide that text from displaying in my post?

    This is the code I am using to print the value for that key:

    I am feeling: <?php $values = get_post_custom_values("Mood"); echo $values[0]; ?>

    Thanks for your help!

Viewing 6 replies - 1 through 6 (of 6 total)
  • EDIT: Need to implode first.

    $mood = implode(get_post_custom_values('mood'));
    if ($mood) {echo "I am feeling $mood";}

    Whoa, mores … this is cool … not really sure what I am doing

    <?php
    
    $mood = implode(get_post_custom_values('mood'));
    if ($mood) {echo "I am feeling $mood";}
    
    ?>

    It will display the value when it is there and then when there is no value to pass for the variable. there is the following error about how it has to be an array :

    Warning: implode() [function.implode]: Argument to implode must be an array. in ____ on line X

    If you could help that’d be great, this is really useful.

    benfranklin, it might be due to your code not initializing the custom values properly.
    Here’s the code that I use with my project. I have a custom value called “endet”, which is the end date of a post. (If an event, for example, lasted a couple of days, I display the posting date and add the end date from the custom value)

    <?php global $post;
    $myposts = get_posts('numberposts=999&' . "category=$category->id");
    foreach($myposts as $post) : setup_postdata($post);
    	$endet = implode(get_post_custom_values('endet'));
    	if ($endet){$endet=" - ".$endet;}
    	if( $post->post_content ) { ?>
    		<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br /><span class="datetime">(<?php the_time('j. F Y'); ?><?php echo $endet; ?>)</span></li>
    	<?php } else { ?>
    		<li><?php the_title(); ?><br /><span class="datetime">(<?php the_time('j. F Y'); ?><?php echo $endet; ?>)</span></li>
    	<?php } ?>
    <?php endforeach; ?>

    Hope this helps.

    Mores, thanks but that’s not exactly the issue … used your code with my own value / variable and posted it in my single.php page in my template …. and it works fine at showing the custom value when there is one assigned for a post but when there is not one assigned for “screen” for a particular post I get the error below …

    <?php
    
    $screen = implode(get_post_custom_values('screen'));
    if ($screen) {echo "$screen";}
    
    ?>
    
    Warning: implode() [function.implode]: Argument to implode must be an array. in progsingle.php on line 50

    Okay, I was being a fool, this re arrangement works:

    <?php
    
    if (get_post_custom_values('screen')) {
           echo implode(get_post_custom_values('screen'));
    }
    
    ?>

    benfranklin, this works perfectly… thanks so much for the help. is it possible to arrange that in a list form and add css styles? such as:

    <div id="classHere">
    <ul>
    <li><?php
    
           if (get_post_custom_values('screen1')) {
           echo implode(get_post_custom_values('screen1'));
    }
    </li>
    <li><?php
    
           if (get_post_custom_values('screen2')) {
           echo implode(get_post_custom_values('screen2'));
    }
    </li>
    </ul>
    ?>

    just wondering. the solution you posted above is going to help me throughout my entire site. thanks again!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Hiding if custom field is empty’ is closed to new replies.