如何从 WordPress 的分类中获取帖子?

所以我有一个名为(“knowledge_base”)的自定义类型,它有一个名为(“section”)的分类法,其中之一是(“狗咬”)。现在我在 example.com/section/dog-bite/ 上,我正在尝试显示此处的帖子。这是我到目前为止所拥有的,所以我不确定缺少什么,但它只显示所有部分的所有帖子。


$current = get_queried_object();


 $args = array(

        'post_type' => 'knowledge_base',

        'tax_query' => array(

            array(

             'taxonomy'  => 'section',

             'field'    => $current->slug,

             'terms'    => $current->name

             )

         )

       );

 // The Query

 $the_query = new WP_Query( $args );


if ( $the_query->have_posts() ) {

     echo '<ul>';

     while ( $the_query->have_posts() ) {

         $the_query->the_post();

         echo '<li>' . get_the_title() . '</li>';

     }

     echo '</ul>';

 } else {

     // no posts found

 }

应该只有2个帖子


呼唤远方
浏览 101回答 1
1回答

慕姐4208626

检查此代码。$args = array(&nbsp; &nbsp; 'post_type' => 'knowledge_base',&nbsp; &nbsp; 'tax_query' => array(&nbsp; &nbsp; &nbsp; &nbsp; array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'taxonomy'&nbsp; => 'section',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'field'&nbsp; &nbsp; => 'slug', // ‘term_id’, ‘name’, ‘slug’ or ‘term_taxonomy_id’&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'terms'&nbsp; &nbsp; => $current->slug, // It's will be $term->slug&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; ));// The Query$the_query = new WP_Query( $args );if ( $the_query->have_posts() ) {&nbsp; &nbsp; echo '<ul>';&nbsp; &nbsp; while ( $the_query->have_posts() ) {&nbsp; &nbsp; &nbsp; &nbsp; $the_query->the_post();&nbsp; &nbsp; &nbsp; &nbsp; echo '<li>' . get_the_title() . '</li>';&nbsp; &nbsp; }&nbsp; &nbsp; echo '</ul>';}
打开App,查看更多内容
随时随地看视频慕课网APP