繁体   English   中英

WordPress的按类别排序帖子

[英]Wordpress Sort Posts By Category

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

我有两个类别:“事件”和“新闻”。 我希望用户能够单击“事件”按钮,并使所有帖子消失,除了事件帖子,还有一个“新闻”和一个“全部”。

在插件上寻找代码解决方案。 干杯。

    <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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM