特定のページ 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();
}
}
}
?>
ただし、次の 2 つの問題が残っています。
- プルされる写真の合計量を制限します。「numberposts」を使用すると、各投稿から取得される画像の量のみが制限されます
- ランダム化。Orderby => rand は、各投稿内の画像のみをランダム化します。すべての順序をランダムにシャッフルしたいと思います。