如何从特定页面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();
}
}
}
?>
但是,还有两个问题:
- 限制提取的照片总数。 使用"数字帖子"仅限制从每个帖子中提取的图像数量
- 随机化。 Orderby => 兰德只随机化每个帖子中的图像。 我想随机洗牌所有内容的顺序。
尝试使用 get_pages( $args )
<?php $args = array(
'child_of' => 'SPECIFIC PAGE',
'parent' => 'SPECIFIC PAGE',
'post_type' => 'attachment',
'post_status' => 'publish',
'numberposts' => -1
); ?>
使用child_of
将得到所有的孩子和孙子。
parent
将此限制为仅将其作为父母的孩子。 没有孙子。
有关更多详细信息,请参阅此处。 http://codex.wordpress.org/Function_Reference/get_pages