这里的方法去实现它:
首先下面的代码添加到你的主题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)标题不是您在后端输入的标题。 –