這裏有一點幫助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_posts
get_children
工作正常。但我需要pre_get_posts
來過濾查詢。任何想法或建議?
get_children調用之前,var_dump($ post-> ID)的輸出是什麼? – Ben