gpt4 book ai didi

javascript - WordPress 集成 FlexSlider 不显示标题

转载 作者:行者123 更新时间:2023-11-27 23:33:14 26 4
gpt4 key购买 nike

在有人将我链接到此之前 EXTREMELY similar (if not identical) question ,那里提供的答案不适用于我的代码。

我试图通过发现的函数做所有事情 here

但是,当且仅当附加图像有标题时,我似乎无法弄清楚如何添加标题,更不用说如何访问附加图像的标题了。我有一种感觉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;

但是,如果您引用管理中心中设置的标题,则需要使用 post_excerpt:

$attachment = get_post( $attachment_id );
$caption = $attachment->post_excerpt;

echo $caption;

关于javascript - WordPress 集成 FlexSlider 不显示标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34364219/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com