Forums

Guide: How To Have A Default Thumbnail (1 post)

  1. plz.help.me
    Member
    Posted 2 years ago #

    Trying to find a way to have a default thumbnail if you cannot provide one for a post or don't want to add a thumbnail to all of your old posts...

    I know I did.

    Here is how you can have a default thumbnail:

    Try to follow the code below and if you need any help, contact me at my site (http://www.graysonadams.com).

    <?php
    $key = get_post_meta($post->ID, 'NAME OF CUSTOM FIELD FOR IMAGE', true);
    if($key) { ?>
    <img src="<?php echo $key; ?>" alt="Post Pic" width="590" height="280"/>
    <?php } else { ?>
    <img src="<?php bloginfo( 'template_directory' ); ?>/images/no-image-available2.jpg" alt="Post Pic" width="590" height="280" />
    <?php } ?>

    Breaking Down The Code

    <?php
    $key = get_post_meta($post->ID, 'NAME OF CUSTOM FIELD FOR IMAGE', true);
    if($key) { ?>

    Replace NAME OF CUSTOM VALUE FOR IMAGE with the custom field name that is set for your thumbnails.

    <img src="<?php echo $key; ?>"/>
    This tells 'img' to read the custom field in order to get the URL of the image.

    <?php } else { ?>
    <img src="IMAGE URL GOES HERE" />
    <?php } ?>

    This translates to: If there is no value found under the custom field use the image below.

    MAKE SURE YOU DO NOT REMOVE THE '<?php' LINES!

    Questions? Contact me at my website: http://www.graysonadams.com/

Topic Closed

This topic has been closed to new replies.

About this Topic

  • RSS feed for this topic
  • Started 2 years ago by plz.help.me
  • This topic is not a support question
  • WordPress version: 2.9.1

Tags

No tags yet.