![](/img/trans.png)
[英]FlexSlider not showing up at all and captions are not showing correctly?
[英]WordPress Integrated FlexSlider not showing captions
在任何人将我链接到这个非常相似(如果不相同)的问题之前 ,在那里提供的答案不适用于我的代码。
我试图通过这里找到的功能来做所有事情
但是,我似乎无法弄清楚如何添加标题,如果并且仅在附加图像包含一个标题的情况下,如何访问附加图像的标题。 我有一种感觉wp_prepare_attachment_for_js()
是访问附加图像标题的方式,但是我在编写函数时非常wp_prepare_attachment_for_js()
,甚至不知道如何在现有函数中利用它。
我当前的functions.php:
//Add Flexslider
function add_flexslider() {
global $post;
$attachments = get_children ( array(
'post_parent' => $post->ID,
'order' => 'ASC',
'orderby' => 'menu_order',
'post_type' => 'attachment',
'post_mime_type' => 'image',
));
if ($attachments) {
echo '<div class="flexslider">';
echo '<ul class="slides">';
foreach ( $attachments as $attachment_id => $attachment ) {
echo '<li>';
echo wp_get_attachment_image($attachment_id, 'large');
//if statement that shows the caption only if attached image has one
echo '<p class="flex-caption">';
//somehow get attached image's caption. perhaps with wp_prepare_attatchment_for_js()?
echo '</p>';
//end if caption statement
echo '</li>';
}
echo '</ul>';
echo '</div>';
}
}
有很多方法可以做到这一点... wp_get_attachment_metadata()
是其中一种:
$metadata = wp_get_attachment_metadata( $attachment_id );
$caption = $metadata ? $metadata['image_meta']['caption'] : '';
echo $caption;
但是,如果您引用的是在Admin中设置的标题,则需要使用post_excerpt
:
$attachment = get_post( $attachment_id );
$caption = $attachment->post_excerpt;
echo $caption;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.