0

I am bringing in a custom field onto my Wordpress template page as follows, but would like to know of how I could display an image in its place if there is no custom field inserted...

<?php 
    $thumbnail_id = get_post_meta($post->ID, 'thumbnail_id', true);
    echo wp_get_attachment_image($thumbnail_id, 'thumbnail');
?>

UPDATE: kinda got this working like this:

 <?php $thumbnail_id = get_post_meta($post->ID, 'thumbnail_id', true);
//Checking if anything exists for the custom field thumbnail id
if ($thumbnail_id) { ?>
 <?php echo wp_get_attachment_image($thumbnail_id, '');?>
<?php } //if there is nothing for thumbnail then display
else { ?>
<img src="yoururlimage.com/image.jpg" /><?php } 
?>

2 Answers 2

1

UPDATE: kinda got this working like this:

<?php $thumbnail_id = get_post_meta($post->ID, 'thumbnail_id', true);
//Checking if anything exists for the custom field thumbnail id
if ($thumbnail_id) { ?>
<?php echo wp_get_attachment_image($thumbnail_id, '');?>
<?php } //if there is nothing for thumbnail then display
else { ?>
<img src="yoururlimage.com/image.jpg" /><?php } 
?>
Sign up to request clarification or add additional context in comments.

Comments

0
<?php if ($thumbnail_id = get_post_meta($post->ID, 'thumbnail_id',     true)){
echo $thumbnail_id;}
else{echo wp_get_attachment_image($thumbnail_id, 'thumbnail');}?>

4 Comments

hmm thanks, when I insert that code in place of what I have, it doesn't come up with the image s at all though...
it echos out the custom field value so use the <img> tags
This code snippet would be more helpful if you pointed out what change needed to be made.
ah, thanks... kinda got me on the right track. I had to mess with my original code a bit so that it worked. Changes made above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.