【发布时间】:2012-06-15 18:06:48
【问题描述】:
如何从特定页面 ID 的所有子页面中检索附件?
例子:
特定页面
- 孩子(带附件)
- 孩子(带附件)
- 孩子(带附件)
我目前正在使用此代码来检索站点范围内的所有附件,但是我想将其限制为仅从特定页面的所有子项中提取图像。
<?php
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => null );
$attachments = get_posts( $args );
if ($attachments) {
foreach ( $attachments as $post ) {
setup_postdata($post);
the_title();
the_attachment_link($post->ID, false);
the_excerpt();
}
}
?>
按照尼克的建议,几乎可以使用此代码:
<?php
$mypages = get_pages('child_of=19');
foreach ( $mypages as $mypage ) {
$attachments = get_children(array('post_parent' => $mypage->ID, 'numberposts' => 1, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'rand'));
if ($attachments) {
foreach ( $attachments as $post ) {
setup_postdata($post);
the_title();
the_attachment_link($post->ID, false);
the_excerpt();
}
}
}
?>
但是,还有两个问题:
- 限制提取的照片总数。使用“numberposts”只会限制从每个帖子中提取的图片数量
- 随机化。 Orderby => rand 仅随机化每个帖子中的图像。我想随机打乱所有东西的顺序。
【问题讨论】:
-
以防万一我建议您将此问题移至 WordPress 站点,地址为 wordpress.stackexchange.com