• Guys, I have following problem. I want my theme to show thumbnail image for posts on my frontpage. For this I have a code:

    <img class="thumbnail" src="<?php echo get_option('home'); ?>/wp-content/themes/mytheme/img/<?php
    // this is where the custom field prints images for each Feature
    $values = get_post_custom_values("Image"); echo $values[0]; ?>" alt=""/>

    What do I need to add to make my posts not to show thumbnail images if I don’t want them, like “if” “ifelse” something like that?

Viewing 2 replies - 1 through 2 (of 2 total)
  • I always use this if else code in order to display my custom fields:

    <?php if ( get_post_meta($post->ID, "your-key-here", true) ) { ?>
    
    <img src="<?php echo get_post_meta($post->ID, "your-key-here", true); ?>" class="thumbnail" alt="Your alt text" />
    
    <?php } else { ?>
    
    <!-- do nothing -->
    
    <?php } ?>

    This basically first checks to see if the custom field is present. If it is it then display it in an img tag to display the image, if not then it does nothing. For this to work the custom field value must be the absolute URL of the image.

    Thread Starter yerlan

    (@yerlan)

    equalmark!
    Thanks a bunch, man! I have used piece of your code, piece of mine and finally found out the right way!

    All the best!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Custom fields question’ is closed to new replies.