Wordpress按类别排序帖子

时间:2017-12-05 09:52:09

标签: php wordpress

我按日期顺序输出所有帖子,并希望添加一些按钮,允许用户通过类别对其进行排序。

我所拥有的两个类别是“事件”和“新闻”。我希望用户能够单击“事件”按钮,并使所有帖子在“活动”帖子以外的所有帖子中消失,还有一个用于“新闻”,一个用于“全部”。

寻找插件的代码解决方案。欢呼声。

1 个答案:

答案 0 :(得分:0)

    <div class="filter-dropdown">
      <?php $args = array('type' => 'CUSTOM_POST_TYPE','orderby' => 'name','order' => 'ASC','taxonomy' => 'TAXONOMY_NAME' ); ?>
      <?php $terms = get_terms( array( 'taxonomy' => 'TAXONOMY_NAME', 'parent'   => 0 ) );
      $allcats = array();
      ?>
<!-- Nav Tabs -->
    <ul class="nav nav-tabs" role="tablist">
              <li class="active" role="presentation">
                <a href="#tab1" aria-controls="tab1" role="tab" data-toggle="tab" aria-expanded="true">ALL</a>
              </li>
      <?php  $i=1; 
        $class=''; 
          foreach ($terms as $category) { 
            if ($category->category_parent == 0 && $category->slug != 'all') { 
              $i++; 
                array_push($allcats,$category->slug); 
                  echo '<li role="presentation" class=""><a href="#tab'.$i.'" aria-controls="tab'.$i.'" role="tab" data-toggle="tab">'.$category->name.'</a></li>';
            } 
          } 
      ?>
    </ul>
<!-- -->
    </div>

    <div class="tab-content">
      <?php $post_type = 'CUSTOM_POST_TYPE'; $taxonomy = 'TAXONOMY_NAME'; ?>
<!-- Code to Display All Posts -->
        <div id="tab1" class="tab-pane fade active in" role="tabpanel">
          <div class="row">
            <?php $args = array( 'post_type' => $post_type, 'posts_per_page' => -1, 'tax_query' => array(array('taxonomy' => $taxonomy, 'field'    => 'slug', 'terms'    => $allcats, 'include_children' => false, ),),);
              $posts = new WP_Query($args);
              $j = 0;
                if( $posts->have_posts() ){ 
                  while( $posts->have_posts() ) { 
                    $posts->the_post(); 
                      $image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'full' ); ?>
                      <div class="col-sm-6 col-xs-6">
                        <div class="tab_pic">
                          <img src="<?php echo $image[0]; ?>" alt="" />
                          <a href="<?php the_permalink(); ?>" class="tab_hover">
                            <span class="inner_txt"><?php the_title(); ?></span>
                          </a>
                        </div>
                      </div>
                  <?php }
                }
            wp_reset_query(); ?>
          </div>
        </div>
<!-- -->
        <?php $i=1; $class=''; 
          foreach( $allcats as $cats ) {
            $postcat= $category->cat_ID; $i++; ?>
              <div role="tabpanel" class="tab-pane fade" id="tab<?php echo $i; ?>">
                <div class="row">
                  <?php $args = array( 'post_type' => $post_type, 'posts_per_page' => -1, 'tax_query' => array(array('taxonomy' => $taxonomy,'field' => 'slug', 'terms' => $cats, 'include_children' => false,) ) );
                    $posts = new WP_Query($args); if( $posts->have_posts() ){ while( $posts->have_posts() ) { $posts->the_post();
                    $image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'full' ); ?>
                      <div class="col-sm-6 col-xs-6">
                        <div class="tab_pic">
                          <img src="<?php echo $image[0]; ?>" alt="" />
                          <a href="<?php the_permalink(); ?>" class="tab_hover">
                            <span class="inner_txt"><?php the_title(); ?></span>
                          </a>
                        </div>
                      </div>
                  <?php } } wp_reset_query(); ?>
                </div>
              </div>
              <?php } ?>
            </div>