如何按自定义帖子类型显示所有帖子类别


How to display all posts category wise of a custom post type

我正在一个学院网站上工作,作为课程。我想将所有帖子显示为自定义帖子类型的列表类别。例如:

类别名称1
- 类别名称
1的帖子 - 类别名称

1的帖子 类别名称2
- 类别名称2
的帖子 - 类别名称2
的帖子

等等

下面是显示所有

类别中自定义帖子类型的所有帖子的代码,但我想单独显示它们类别,请帮助我......

<?php
$type = 'course';
$args=array(
  'post_type' => $type,
  'post_status' => 'publish',
  'posts_per_page' => -1,
  'caller_get_posts'=> 1);

$my_query = '';
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
  while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    <?php
  endwhile;
}
wp_reset_query();
?>

您可能需要多次执行查询,或者每个类别执行一次查询,键是在查询中添加"category_name"=>"slug_name"


    <?php
// query category 1   
 $type = 'course';
    $args1=array(
      'post_type' => $type,
      'post_status' => 'publish',
      'posts_per_page' => -1,
    'category_name' => 'slug_name' // added the category name enter the slug name as defined in the category
      'caller_get_posts'=> 1);
// query category 2   
 $type = 'course';
    $args2=array(
      'post_type' => $type,
      'post_status' => 'publish',
      'posts_per_page' => -1,
    'category_name' => 'slug_name' // added the category name enter the slug name as defined in the category
      'caller_get_posts'=> 1);
    $my_query = '';
    $my_query = new WP_Query($args1);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();
   $my_query = '';
    $my_query = new WP_Query($args2);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();
    ?>

> 100% 使用此代码

$tax_query[] = array(
                 'taxonomy'      => '< taxonomy_slug >',
                'field' => 'term_id', 
                'terms'         => < terms_ids >, // array(12,13,...)
            );
            $args = array(
                'post_type'      => '< post type name>',
                'posts_per_page' => 10,
                'tax_query' => $tax_query
            );
          $loop = new WP_Query( $args );
          while ( $loop->have_posts() ) : $loop->the_post();
            echo  get_the_ID();
          endwhile;