WordPress显示由自定义分类过滤的CPT

时间:2016-04-07 11:56:26

标签: php mysql wordpress

我正在显示自定义帖子类型的所有帖子,但我想按分类法显示它们。

我希望它看起来像这样:

第1学期:

  
      
  • post x
  •   
  • post y
  •   

第2学期:

  
      
  • post z
  •   
     

第3学期:

     
      
  • post n
  •   

我的计划是为每个分类法分别制作3个wp_queries,因为我有3个,但是我有一些麻烦设置我的查询。

这就是我的查询:

$posts = new WP_Query(array(
        'post_type' => 'job',
        'tax_query' => array(
                    array(
                        'taxonomy' => 'taxonomy',
                        'field' => 'slug',
                        'terms' => 'slug',
                    ),
        ),
        'posts_per_page' => 10
    ));

进行3次查询似乎有点愚蠢,是否有更好的方法可以使用1个查询来执行此操作?

2 个答案:

答案 0 :(得分:2)

  

你也可以这样做。

 <?php

     $cat = get_terms('category'); // you can put your custom taxonomy name as place of category.
        foreach ($cat as $catVal) {
            echo '<h2>'.$catVal->name.'</h2>';
            $postArg = array('post_type'=>'post','posts_per_page'=>-1,'order'=>'desc',
                              'tax_query' => array(
                                                    array(
                                                        'taxonomy' => 'category',
                                                        'field' => 'term_id',
                                                        'terms' => $catVal->term_id
                                                    )
                            ));

            $getPost = new wp_query($postArg);
            global $post;

            if($getPost->have_posts()){
                echo '<ul>';
                    while ( $getPost->have_posts()):$getPost->the_post();
                        echo "<li>".$post->post_title."</li>";
                    endwhile;
                echo '</ul>';
            }

        }
    ?>

输出 enter image description here

答案 1 :(得分:0)

您可以选择更多分类

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => array( 'quotes' ),
        ),
        array(
            'taxonomy' => 'post_format',
            'field'    => 'slug',
            'terms'    => array( 'post-format-quote' ),
        ),
    ),
);
$query = new WP_Query( $args );

之后,您需要解析一次您的结果以制作您想要的数组。

然后在显示信息之后在此数组上循环

或者你可以(但我不喜欢这种方式)将你的html存储在php var

Ex 1:

<?php 
      while($posts->have_posts()): $posts->the_post(); ?>
          <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
      <?php
             endwhile;
             wp_reset_postdata();
      ?>


    $jobList = array();


    $posts = $posts->get_posts();

    foreach($posts as $post)
    {
        $jobList[$post['yourTaxFieldName']][] = $post;
    }
    // and you can now loop on $jobList for display in order your jobs categories

Ex 2 :(工作不好但工作正常)

$job1 = '';
$job2 = '';
$job3 = '';

while($posts->have_posts()): $posts->the_post();
    if($yourTaxFieldName == 1)
        $job1 .= '<li>'.$your_content.'</li>';
    elseif ($yourTaxFieldName == 2) 
    {
        $job2 .= '<li>'.$your_content.'</li>';
    }
    else {
        $job3 .= '<li>'.$your_content.'</li>';
    }
   endwhile;
   wp_reset_postdata();

echo '<ul><h1>First job : </h1> ',$job1,' </ul>'; 
// etc.
相关问题