簡體   English   中英

如何從 wordpress 的分類中獲取帖子?

[英]How can I get the posts from a taxonomy in wordpress?

所以我有一個名為(“knowledge_base”)的自定義類型,它有一個名為('section')的分類法,其中之一是('dog beat')。 現在我在 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個帖子

檢查此代碼。

$args = array(
    'post_type' => 'knowledge_base',
    'tax_query' => array(
        array(
            'taxonomy'  => 'section',
            'field'    => 'slug', // ‘term_id’, ‘name’, ‘slug’ or ‘term_taxonomy_id’
            'terms'    => $current->slug, // It's will be $term->slug
        )
    )
);
// 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>';
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM