仅在标题存在时显示图像标题 - Wordpress

时间:2014-04-11 18:49:56

标签: php wordpress function caption

我在这里找到了一个非常有用的讨论,概述了如何显示图像标题,alt,标题等。我已经测试了这个功能,它运行良好,但对于没有字幕的图像,div标题仍然显示。如果没有可用的标题,我该如何才能显示此功能?

function the_post_thumbnail_caption() {
  global $post;

  $thumb_id = get_post_thumbnail_id($post->id);

  $args = array(
'post_type' => 'attachment',
'post_status' => null,
'post_parent' => $post->ID,
'include'  => $thumb_id
); 

   $thumbnail_image = get_posts($args);

   if ($thumbnail_image && isset($thumbnail_image[0])) {
     //show thumbnail title
     echo $thumbnail_image[0]->post_title; 

     //Uncomment to show the thumbnail caption
     //echo $thumbnail_image[0]->post_excerpt; 

     //Uncomment to show the thumbnail description
     //echo $thumbnail_image[0]->post_content; 

     //Uncomment to show the thumbnail alt field
     //$alt = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true);
     //if(count($alt)) echo $alt;
  }
}

1 个答案:

答案 0 :(得分:0)

if ($thumbnail_image[0]->post_excerpt != '')
{ 
    echo $thumbnail_image[0]->post_excerpt; 
}