• Hello WP fans,

    I’ve been struggling to set up a default image on posts using a custom field and was wondering if anyone could assist.

    Here is the code:

    <?php
         $Image = get_post_meta($post->ID,'Image', true);
         $Image = ( !empty( $Image ) ) ? $Image : 'https://www.mysite.com/wp-content/uploads/2010/09/placeholderimage-14.jpg';
                 print '<img class="pageimg" src="' . $Image . '" width="183" height="196" alt="" />';
        ?>

    I believe I’ve missed something, but my PHP skills are seriously lacking… The regular image is showing up, however the default image does not show for those posts without a custom image.

    Thanks in advance for your thoughts!

    Emma

Viewing 3 replies - 1 through 3 (of 3 total)
  • Try:

    <?php
    $image = get_post_meta($post->ID,'Image', true);
    if( $image == '' ) $image = get_bloginfo('url') . '/wp-content/uploads/2010/09/placeholderimage-14.jpg';
    echo '<img class="pageimg" src="' . $image . '" width="183" height="196" alt="" />';
    ?>
    Thread Starter WeeEmma

    (@weeemma)

    Brilliant! Thanks Esmi… that and taking out the faux image placeholder helped.
    Who do I write to to get you a raise! ??

    Have a lovely day,

    Emma

    Glad to have helped. ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘get_post_meta default image’ is closed to new replies.