0

A little help here with get_children function. pre_get_posts hook filter the main_query. After that I called get_children to get the attachment image but I ended up giving an 0 array result.

function.php

 function cstm_get_posts() {
        if ( is_post_type_archive( 'tent' ) ) {
        $query->set( 'posts_per_page', 20 );
        return;
       }
     }
 add_action('pre_get_posts', 'cstm_get_posts' );

archive.php

 $attachments = get_children( array(
    'numberposts' => 1,
    'order'=> 'ASC',
    'post_mime_type' => 'image',
    'post_parent' => $post->ID,
    'post_type' => 'attachment'
    ));
foreach($attachments as $attch){
 code display image here
}

Code above is just a snippet.

Anyway if I don't use pre_get_posts the get_children works fine. But I need the pre_get_posts to filter the query. Any idea or suggestion?

4

1 回答 1

1

尝试这个

function cstm_get_posts($query) {
  if ( is_post_type_archive( 'tent' ) &&  $query->is_main_query() ) {
    $query->set( 'posts_per_page', 20 );
    return;
  }
}

将参数添加$query到函数中。

于 2012-11-09T02:07:43.310 回答