Forums

Only show an image if the custom field exists (2 posts)

  1. Turts
    Member
    Posted 2 weeks ago #

    Hi

    I'm trying to make it so that if a post has an image linked to it via a custom field, then it is displayed above the post. If there is no image linked to the file, then nothing is displayed. As the formatting for images in this div uses a border, when there is no image linked you get a 1px wide box above the post. I know I need to use an if statement, but I must be getting the syntax wrong as it either doesn't do anything, or stops the page from parsing. Appreciate any help.

    <?php $image = get_post_meta($post->ID, 'image', true); ?>
    			<img src="<?php echo $image; ?>"/>
    			<h2><?php the_title(); ?></h2>
    				<?php the_content('<p class="serif">Read the rest of this entry &raquo;</p>'); ?>
  2. Icethrill
    Member
    Posted 2 weeks ago #

    <?php if($image != '') ?> <img src="<?php echo $image; ?>"/>

    What what you write in the custom field should be the image url.

Reply

You must log in to post.

About this Topic