我如何從特定頁面ID的所有子頁面檢索附件?如何從WordPress的特定頁面的子頁面檢索附件?
實施例:
特定頁面
- 兒童(帶有附件)
- 兒童(帶有附件)
- 兒童(帶有附件)
我目前使用此代碼檢索網站範圍內的所有附件,但我想限制這只是從中拉圖像一個特定頁面的所有孩子。
<?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網站,請訪問http://wordpress.stackexchange.com/ – leopic