【发布时间】:2015-01-04 17:08:12
【问题描述】:
我正在寻求帮助,由于某种原因,此代码在尝试在 index.php 主页面上显示总图像附件时不起作用。
// Get all the attachments
$attachments = get_posts ( array(
'numberposts' => -1,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_parent' => get_the_ID(),
'post_status' => 'inherit',
) );
// Count all the attachments
$total = count( $attachments );
问题是,循环在 index.php 主页面中,但它正在调用 post.php 模板,所以我把这段代码放在那里(我假设它仍在循环中?)然后我打电话$total 就在我要显示“查看所有 # 张图片”的下方。
任何想法为什么即使我使用基本的 Wordpress 图库媒体库在帖子中添加了一个图片库,这只是将数字显示为 0?
谢谢
【问题讨论】:
-
如果添加
print_r($attachments);,页面上是否打印了任何内容? -
它实际上似乎最终奏效了。我们用来尝试计数的图像似乎有问题。感谢您的帮助。
标签: wordpress count attachment