简体   繁体   English

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

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

So I have a custom type called ("knowledge_base") which has a taxonomy called ('section') and one of them is ('dog bite').所以我有一个名为(“knowledge_base”)的自定义类型,它有一个名为('section')的分类法,其中之一是('dog beat')。 Right now I am on example.com/section/dog-bite/ and I am trying to show the posts located here.现在我在 example.com/section/dog-bite/ 上,我正在尝试显示位于此处的帖子。 This is what I have so far so I am not sure what is missing but it just displays ALL the posts from all sections.这是我到目前为止所拥有的,所以我不确定缺少什么,但它只显示所有部分的所有帖子。

$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
 }

There is supposed to be only 2 posts应该只有2个帖子

Check this code.检查此代码。

$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