Wordpress - 条件自定义场图像

时间:2014-05-05 21:11:13

标签: php wordpress custom-fields advanced-custom-fields

我将自定义字段引入我的Wordpress模板页面,如下所示,但是如果没有插入自定义字段,我想知道如何在其位置显示图像...

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

更新:有点像这样工作:

 <?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 个答案:

答案 0 :(得分:1)

更新:有点像这样工作:

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

答案 1 :(得分: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');}?>