Question

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 } 
?>
Was it helpful?

Solution

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 } 
?>

OTHER TIPS

<?php if ($thumbnail_id = get_post_meta($post->ID, 'thumbnail_id',     true)){
echo $thumbnail_id;}
else{echo wp_get_attachment_image($thumbnail_id, 'thumbnail');}?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top