2012-11-09 24 views
0

這裏有一點幫助get_children函數。 pre_get_posts hook過濾main_query。之後,我打電話給get_children以獲取附件圖像,但最終給出了一個0數組結果。wordpress get_children將無法工作pre_get_posts

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 
} 

代碼只是一個片段。

無論如何,如果我不使用pre_get_postsget_children工作正常。但我需要pre_get_posts來過濾查詢。任何想法或建議?

+1

get_children調用之前,var_dump($ post-> ID)的輸出是什麼? – Ben

回答

1

試試這個

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

添加參數$query的功能。

+0

沒有希望。仍然不會工作.. pre_get_posts工作正常顯示帖子,但get_children不工作0數組返回.. – user1804782

+0

編輯。現在檢查。 – air4x

+0

仍然get_children不工作.. – user1804782