0

特定のページ 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 つの問題が残っています。

  1. プルされる写真の合計量を制限します。「numberposts」を使用すると、各投稿から取得される画像の量のみが制限されます
  2. ランダム化。Orderby => rand は、各投稿内の画像のみをランダム化します。すべての順序をランダムにシャッフルしたいと思います。
4

1 に答える 1

1

使ってみて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

于 2012-06-15T18:59:19.497 に答える