-2

I am using this code to retrieve post meta:

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.

  • 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
  • 1
    If the below answer did answer your question please accept it. – DᴀʀᴛʜVᴀᴅᴇʀ Jun 17 '16 at 13:51
2

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.

0

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";
    }
  • sorry it's not working. It's just the same as before. – user1666698 Oct 9 '12 at 7:57
0

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; 
    }
?>
  • 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
0

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>';
                    } ?>
  • Thanks monirul but I am sorry no change. Still the same problem. – user1666698 Oct 9 '12 at 8:08
0

I was running into the same issue and managed to overcome it by replacing this code:

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

with the following:

(get_post_meta(get_the_ID(),'a',true)

Hope that will save somebody the trouble of getting stuck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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