gpt4 book ai didi

wordpress - 如何在图片附件页面上显示附加到帖子的图片数量?

转载 作者:行者123 更新时间:2023-12-03 17:20:56 25 4
gpt4 key购买 nike

我使用图像附件页面以幻灯片形式一张一张地显示附加到帖子的图像。我希望能够显示附加到父帖子的图像总数以及任何给定附件页面上显示的特定图像的数量,以便您看到图片和文字 “第 3 个图像,共 15 个”例如。

更新... 我能够使用以下代码获得要显示的总数:

<?php 
global $post;
$attachments = get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) );
$count = count( $attachments );
echo $count;
?>

我仍然无法弄清楚如何显示当前图像的编号。
有人有什么建议吗?

更新 2...

格林尼的回答让我几乎到了那里,但它一次输出所有数字:

"Image 1 of 8Image 2 of 8Image 3 of 8Image 4 of 8Image 5 of 8Image 6 of 8Image 7 of 8Image 8 of 8"



这是我使用的代码:
global $post;
$attachments = get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) );
$count = count( $attachments );
$currentImage = 1;
foreach ($attachments as $attachment) {
// output your image here
echo "Image ". $currentImage . " of ". $count;
$currentImage++;
}

怎么了?

更新 3 - 答案!
global $post;
$attachments = get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) );

$count = count( $attachments );
$specific = array();
$i = 1;

foreach ( $attachments as $attachment ) {
$specific[$attachment->ID] = $i;
++$i;
}

echo "Image {$specific[$post->ID]} of {$count}";

最佳答案

这有效:

global $post;
$attachments = get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) );

$count = count( $attachments );
$specific = array();
$i = 1;

foreach ( $attachments as $attachment ) {
$specific[$attachment->ID] = $i;
++$i;
}

echo "Image {$specific[$post->ID]} of {$count}";

关于wordpress - 如何在图片附件页面上显示附加到帖子的图片数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2485011/

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