WordPress 显示带有类别的自定义帖子类型的帖子


Wordpress show posts from custom post type with a category

我正在使用CPT UI插件,我用它创建了一个自定义帖子类型(称为知识库和称为知识库类别的分类法)

我正在使用此代码显示帖子:

<?php $query = new WP_Query( array('post_type' => 'knowledgebase', 'posts_per_page' => 20, 'category_name' => 'Cisco' ) ); ?>
        <?php while ( $query->have_posts() ) : $query->the_post(); ?>
            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <a href=""><?php the_title(); ?></a>
            </article> <!-- .et_pb_post -->
        <?php endwhile; ?>

它在没有category_name的情况下工作正常,但使用上面的代码它不显示任何帖子

有些帖子的类别为 Cisco

愿这会帮助你

<?php $query = new WP_Query( array( 
        'post_type' => 'knowledgebase', 
        'cat' => 5, // Whatever the category ID is for your aerial category
        'posts_per_page' => 10,
        'orderby' => 'date', // Purely optional - just for some ordering
        'order' => 'DESC' // Ditto
    ) );
    while ( $loop->have_posts() ) : $loop->the_post(); ?>

在这里你可以通过这个来实现

$args = array(
    'post_type' => 'knowledgebase',
    'tax_query' => array(
        array(
            'taxonomy' => 'knowledgebase-categories',
            'field'    => 'slug',
            'terms'    => 'knowledgebase-terms',
        ),
    ),
);
$query = new WP_Query( $args );

因为它们是来自自定义帖子类型的帖子,我不得不category_name更改为分类法(即知识库类别)