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

时间:2016-03-09 06:01:15

标签: php wordpress

我正在研究所的网站上作为课程。 我希望将所有帖子显示为自定义帖子类型的列表类别。 例如:

  

CATEGORY NAME1
    - 类别名称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();
?>

2 个答案:

答案 0 :(得分:0)

您可能需要多次进行查询,或者每个类别按一次'category_name'=&gt; '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();
    ?>

答案 1 :(得分:0)

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;