2011-06-06 61 views

回答

0

我計算出來:

/************************************************************\ 
* Fetch The Post Thumbnail Caption 
\************************************************************/ 

function the_post_thumbnail_caption() { 
    global $post; 

    $thumbnail_id = get_post_thumbnail_id($post->ID); 
    $thumbnail_image = get_posts(array('p' => $thumbnail_id, 'post_type' => 'attachment')); 

    if ($thumbnail_image && isset($thumbnail_image[0])) { 
    echo $thumbnail_image[0]->post_excerpt; 
    } 
} 
1

下面是一個更簡單的和更短的代碼:

<?php the_post_thumbnail(); 
echo get_post(get_post_thumbnail_id())->post_excerpt; ?> 
+0

尼斯和簡單。 – Foxinni 2013-02-15 13:59:34

0
if(!function_exists('get_post_thumbnail_caption')) { 
    function get_post_thumbnail_caption($post_id = null) { 
     $post_id = (null === $post_id) ? get_the_ID() : $post_id; 
     $thumbnail_id = get_post_thumbnail_id($post_id); 
     if ($thumbnail = get_post($thumbnail_id)) 
      return $thumbnail->post_excerpt; 
     return ''; 
    } 
} 

if(!function_exists('the_post_thumbnail_caption')) { 
    function the_post_thumbnail_caption($post_id = null) { 
     echo get_post_thumbnail_caption($post_id); 
    } 
} 

if(has_post_thumbnail()) { 
    the_post_thumbnail(); 
    the_post_thumbnail_caption(); 

    $caption = get_post_thumbnail_caption(123); 
    if('' == $caption) 
     echo '<div class="caption">'.$caption.'</div>'; 
} 
1

作爲WordPress的4.6的,功能the_post_thumbnail_caption()已被添加到核心(/ WP - 包括/後縮略圖的template.php)。

使用這裏發佈的代碼將導致錯誤:

Fatal error: Cannot redeclare the_post_thumbnail_caption() 
+0

我的主題有一個已經存在於Wordpress(4.6)核心文件中的功能,正如您所提到的。所以我在主題文件中的函數前添加了:「if(!function_exists('the_post_thumbnail_caption')){...},並且修正了它。謝謝! – jake 2016-09-05 09:41:15

+0

非常感謝代碼片段! – 2016-09-07 09:12:25

相關問題