Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using this code to retrieve post meta:

<?php if( get_post_meta($post->ID,'a',true) == ''){
                        echo '<img src="' . get_bloginfo( 'template_url' ) . '/img/img22.jpg" ></img>';
                    }else{
                        echo get_post_meta($post->ID,'a',true); 
                    } ?>

My first block of if statement is always executing no matter whether the mete box text field is empty or not and else block is not executing in any case. I want to know if there is any flaw in this code or

echo get_post_meta($post->ID,'a',true)

is not a valid code. Your help will be highly appreciated.

share|improve this question
1  
If you put var_dump(get_post_meta($post->ID,'a',true)) before the if, what do you get? –  Miha Rekar Oct 9 '12 at 7:26
    
It's displaying this string '' (length=0) –  user1666698 Oct 9 '12 at 7:55
    
Well there is your answer - the post does not have a meta value with key a ;) –  Miha Rekar Oct 9 '12 at 8:13
    
Actually I have a meta value with key a but not figuring out what's getting it not work. –  user1666698 Oct 9 '12 at 8:18
    
No, you do not. If you would have it, than this would return it ;) Try var_dump(get_post_meta($post->ID)) and it will return all the meta values you have. –  Miha Rekar Oct 9 '12 at 8:25
show 1 more comment

4 Answers

I was having some issues like yours. It was because it was on a page template with a query above it. All I had to do was reset the query.

http://codex.wordpress.org/Function_Reference/wp_reset_query

Hope this helps you.

share|improve this answer
add comment

try this.Hope this will work fine

$key_1_value = get_post_meta($post->ID, 'a', true);
    // check if the custum field has a value
    if($key_1_value != '') {
      echo $key_1_value;
    } else{
      echo '<img src="' . get_bloginfo( 'template_url' ) . '/img/img22.jpg";
    }
share|improve this answer
    
sorry it's not working. It's just the same as before. –  user1666698 Oct 9 '12 at 7:57
add comment

Use PHP empty() function as the condition to check whether meta value is null or not:

<?php
    $my_val = get_post_meta($post->ID,'a',true);
    if( empty( $my_val ) ) {
        echo '<img src="' . get_bloginfo( 'template_url' ) . '/img/img22.jpg" ></img>';
    } else {
        echo $my_val; 
    }
?>
share|improve this answer
    
sorry it's not working. It's just the same as before. –  user1666698 Oct 9 '12 at 8:00
    
can you please var_dump(get_post_meta($post->ID,'a',true)) and send the result here so that i might help. –  Md Toufiqul Islam Oct 9 '12 at 9:38
    
This is what is shows "string '' (length=0)" –  user1666698 Oct 9 '12 at 10:41
add comment

Try This

<?php if( ( $usermeta  = get_post_meta($post->ID,'a',true)) && !empty($usermeta) ){
                        echo $usermeta; 

                    }else{
                        echo '<img src="' . get_bloginfo( 'template_url' ) . '/img/img22.jpg" ></img>';
                    } ?>
share|improve this answer
    
Thanks monirul but I am sorry no change. Still the same problem. –  user1666698 Oct 9 '12 at 8:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.