問題描述
我有一個畫廊附在頁面上。在該頁面上,我執行以下查詢:
$events_gallery = new WP_Query( // Start a new query for our videos
array(
'post_parent' => $post->ID, // Get data from the current post
'post_type' => 'attachment', // Only bring back attachments
'post_mime_type' => 'image', // Only bring back attachments that are images
'posts_per_page' => '3', // Show us the first three results
'status' => 'inherit', // Inherit the status of the parent post
'orderby' => 'rand', // Order the attachments randomly
)
);
我已經嘗試了很多方法,由於某些原因,我無法獲得附件返回。我在這裡缺少一些明顯的東西嗎?
*更新
感謝 Wok 指出我正確的方向。
原來我正在使用”status” 而不是”post_status” 。該程式碼以”status” 的”attachment” 後期型別的 in-context 解釋為例。我更新了程式碼,以引用”post_status” 。正確的程式碼如下:
$events_gallery = new WP_Query( // Start a new query for our videos
array(
'post_parent' => $post->ID, // Get data from the current post
'post_type' => 'attachment', // Only bring back attachments
'post_mime_type' => 'image', // Only bring back attachments that are images
'posts_per_page' => '3', // Show us the first result
'post_status' => 'inherit', // Attachments default to "inherit", rather than published. Use "inherit" or "all".
'orderby' => 'rand', // Order the attachments randomly
)
);
最佳解決方案
這些是我使用的查詢引數… 當我迴圈結果時,我可以為我工作
array(
'post_parent' => $post->ID,
'post_status' => 'inherit',
'post_type'=> 'attachment',
'post_mime_type' => 'image/jpeg,image/gif,image/jpg,image/png'
);
次佳解決方案
加入 $args,這很重要。
'post_status' => 'any'
不要:'post_status' => null
這很重要,因為附件沒有 post_status,因此 post_status(published) 的預設值將找不到附件。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。