Php 如何在WordPress中从特定页面的子页面检索附件?
如何从特定页面ID的所有子页面中检索附件 例如: 特定页面Php 如何在WordPress中从特定页面的子页面检索附件?,php,html,wordpress,Php,Html,Wordpress,如何从特定页面ID的所有子页面中检索附件 例如: 特定页面 儿童(带附件) 儿童(带附件) 儿童(带附件) 我目前正在使用此代码检索站点范围内的所有附件,但是我想将此限制为仅从特定页面的所有子级提取图像 <?php $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => null ); $attachment
- 儿童(带附件)
- 儿童(带附件)
- 儿童(带附件)
<?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();
}
}
?>
根据Nick的建议,使用此代码几乎可以达到以下效果:
<?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();
}
}
}
?>
然而,还有两个问题:
get\u页面($args)
使用child\u的
将获得所有子代和孙辈
parent
将此限制为仅将此作为父项的子项。没有孙子
请参阅此处了解更多详细信息 以防我建议您将此问题转移到WordPress站点@Joe,您可以将所有附件数据存储在多维数组中,并将其保存。然后使用
for
循环来显示适当数量的附件。好奇的是,这是否仍然会提取额外的未使用数据(例如,有1000个图像)?我认为这是在浪费服务器的大量工作?您已经在get\u children()
调用中从数据库中提取了数据。现在你只需要对它进行手术。与其在foreach循环中输出,不如将其存储在数组中。然后洗牌数组,然后循环数组以输出所需内容。是的,这是一个多一点的工作,但我想不出一种方法来从get\u children()
或get\u pages()
调用中获得您想要的内容。
<?php $args = array(
'child_of' => 'SPECIFIC PAGE',
'parent' => 'SPECIFIC PAGE',
'post_type' => 'attachment',
'post_status' => 'publish',
'numberposts' => -1
); ?>