這裏的方法去實現它:
首先下面的代碼添加到你的主題functions.php
:
function wp_get_attachment($attachment_id) {
$attachment = get_post($attachment_id);
return array(
'alt' => get_post_meta($attachment->ID, '_wp_attachment_image_alt', true),
'caption' => $attachment->post_excerpt,
'description' => $attachment->post_content,
'href' => get_permalink($attachment->ID),
'src' => $attachment->guid,
'title' => $attachment->post_title
);
}
之後,你可以使用它像:
$attachment_meta = wp_get_attachment($attachment_id);
echo $attachment_meta['caption'];
所以你的最終代碼是:
global $product;
$attachment_ids = $product->get_gallery_attachment_ids();
echo '<div class="flexslider"><ul class="slides">';
foreach($attachment_ids as $attachment_id)
{
echo '<li>';
echo "<img src=".$image_link = wp_get_attachment_url($attachment_id, 'large').">";
echo '<p>';
$attachment_meta = wp_get_attachment($attachment_id);
echo $attachment_meta['caption'];
echo '</p>';
echo '</li>';
}
echo '</ul></div>';
來源:wordpress topic forum
謝謝...但給我一個語法錯誤:「解析錯誤:語法錯誤,意想不到的 '[' 在/var/sites/t/website.co.uk/public_html/ wp-content/plugins/shortcode-exec -php/shortcode-exec-php-class.php(895):eval()'第10行代碼' –
這不會工作,因爲'wp_get_attachment_metadata'會給[EXIF] (http://en.wikipedia.org/wiki/Exchangeable_image_file_format)標題不是您在後端輸入的標題。 –