好的我使用以下代码为我的附件生成标题。问题是如果我在一个页面中有多个图像,则第一个图像的标题显示所有图像。就像我有" Text One"作为我的图像的标题之一,所有的图像显示了这个" Text One"字幕。我该如何解决这个问题?
<?php
$args = array( 'post_type' => 'attachment',
'orderby' => 'menu_order',
'order' => 'ASC',
'post_mime_type' => 'image' ,
'post_status' => null,
'numberposts' => 50,
'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
$caption = $attachment->post_excerpt;
}
}
?>
<p class="project-caption"><?php echo $caption ?></p>
目前有一个滑块,显示图像。 &安培;滑块代码是:
<?php
if($repeater):
foreach($repeater as $r):
?>
<li class="<?php echo $r["fit_to_screen"] ? "img_fit" : ""; ?>">
<?php
if($r["acf_fc_layout"] == "image"):
$html = "<img data-fit='".$r["fit_to_screen"]."' src='".$r["image"]["url"]."' alt='".$r["image"]["alt"]."'/>";
echo apply_filters( 'post_thumbnail_html', $html, $post->ID , $r["image"]["id"], "large" , array("alt"=>$r["image"]["alt"]) );
else:
echo getVideoEmbed($r["video_url"]);
endif;
?>
</li>
<?php
endforeach;
endif;
?>
答案 0 :(得分:1)
你得到相同的标题,因为你在foreach之后显示标题,你应该在foreach中这样做:
<?php
$args = array( 'post_type' => 'attachment',
'orderby' => 'menu_order',
'order' => 'ASC',
'post_mime_type' => 'image' ,
'post_status' => null,
'numberposts' => 50,
'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
$caption = $attachment->post_excerpt;
echo '<p class="project-caption">' . $caption . '</p>';
}
}
?>
答案 1 :(得分:1)
你覆盖循环中的$caption
,所以最后你会得到最后一个标题。
您可以在循环内打印$caption
然后获得所有字幕。
foreach ( $attachments as $attachment ) {
$caption = $attachment->post_excerpt;
echo '<p class="project-caption">'.$caption.'</p>';
}