在 wordpress 中僅顯示自定義帖子型別存檔頁面的父帖子
我的代碼:
$args = array(
'post_type' => 'programs',
'post_parent' => get_the_ID(),
);
$article_posts = new WP_Query($args);
if($article_posts->have_posts()) :
?>
<?php while($article_posts->have_posts()) : $article_posts->the_post();
$post_id = get_the_ID();
$post_link = get_permalink($post_id);
$post_title = get_the_title();
$featured_img_url = get_the_post_thumbnail_url(get_the_ID());
?>
<p> post </p>
<?php endwhile; ?>
<?php else: ?>
Oops, there are no posts.
<?php endif; ?>
<?php echo "</ul>";?>
結果:
“哎呀,沒有帖子。”
uj5u.com熱心網友回復:
根據檔案,如果您只想要頂級帖子(即父母),那么您需要將 設定post_parent為0不是當前頁面的 id。
還要檢查您是否在注冊自定義帖子型別時將'hierarchical'引數設定為true。
wp_reset_postdata在完成回圈后使用函式也是一個好主意!
所以你的代碼應該是這樣的:
$args = array(
'post_type' => 'programs',
'post_parent' => 0,
);
$article_posts = new WP_Query($args);
echo echo "</ul>";
if($article_posts->have_posts()) :
while($article_posts->have_posts()) :
$article_posts->the_post();
$post_id = get_the_ID();
$post_link = get_permalink($post_id);
$post_title = get_the_title();
$featured_img_url = get_the_post_thumbnail_url(get_the_ID());
?>
<p><?php echo $post_title; ?></p>
<?php
endwhile;
?>
<?php
else:
?>
Oops, there are no posts.
<?php
endif;
?>
<?php echo "</ul>";
wp_reset_postdata();
WP_Query檔案
uj5u.com熱心網友回復:
post_parent 引數反之亦然:您需要此 arg 來查找所有父帖子:
'post_parent' => 0, // find parents
作為(非常笨重的)記憶輔助工具:父帖子為空/不存在。
'post_parent' => get_the_ID() //find children
查詢您當前帖子的所有子帖子。父帖子具有此 ID。
請參閱此執行緒:
如何查詢具有子項的帖子(分層自定義帖子型別)?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/342793.html
