如何使用标签对分类进行排序? WordPress的

时间:2018-08-26 18:30:52

标签: php wordpress filter tabs

如何在不使用归档文件的情况下使用标签对分类进行排序?只需重新加载页面(或不重新加载页面)即可按分类法过滤帖子。

HTML:

  <ul class="portfolio-sorting">
    <li><a href="" class="active">All Rooms</a></li>
    <li><a href="">Entertainment</a></li>
    <li><a href="">Restaurants</a></li>
    <li><a href="">Sport</a></li>
    <li><a href="">Service</a></li>
  </ul>

PHP(WordPress循环):

    <?php $gallerys_posts = new WP_Query($args); //The argument $args must be passed [function set_arges]
    wp_reset_query();


if( $gallerys_posts->have_posts() ) :
  while ( $gallerys_posts->have_posts() ) :
    $gallerys_posts->the_post(); ?>

    <div class="col-lg-4 col-md-4 col-sm-6 col-xs-12 shuffle-item filtered">
      <div class="portfolio-item">
        <a href="<?php echo get_permalink(get_the_id());?>">
          <?php if ( get_the_post_thumbnail(get_the_id()) )  { ?>
            <?php echo get_the_post_thumbnail( get_the_id(), array(620, 423 )); ?>
          <?php } ?>
          <div class="portfolio-overlay">
            <div class="caption">
              <?php echo get_title(); ?>
              <span>
                <?php echo get_the_content(); ?>
              </span>
            </div>
          </div>
        </a>
      </div>
    </div>

    <?php endwhile; endif; wp_reset_query();?>

PHP(为循环返回$ args):

<?php function set_arges($type_filter = 'all') {
  switch ($type_filter) {
    case 'all':
      $args= array( 
          'post_type'           => 'gallerys',
          'posts_per_page'      => '-1',
          'tax_query'           => array(
            array(
              'taxonomy' => 'gallerys_type',
              'field'    => 'slug',
              'terms'    => 'all-rooms'
            )
          )
      );
      return $args;
      break;

    case 'entertainment':
        $args= array( 
          'post_type'           => 'gallerys',
          'posts_per_page'      => '-1',
          'tax_query'           => array(
            array(
              'taxonomy' => 'gallerys_type',
              'field'    => 'slug',
              'terms'    => 'entertainment'
            )
          )
        );
        return $args;
        break;

    case 'restaurants':
      $args= array( 
          'post_type'           => 'gallerys',
          'posts_per_page'      => '-1',
          'tax_query'           => array(
            array(
              'taxonomy' => 'gallerys_type',
              'field'    => 'slug',
              'terms'    => 'restaurants'
            )
          )
      );
      return $args;
      break;

    case 'sport':
      $args= array(
          'post_type'           => 'gallerys',
          'posts_per_page'      => '-1',
          'tax_query'           => array(
            array(
              'taxonomy' => 'gallerys_type',
              'field'    => 'slug',
              'terms'    => 'sport'
            )
          )
      );
      return $args;
      break;

    case 'services':
      $args= array( 
          'post_type'           => 'gallerys',
          'posts_per_page'      => '-1',
          'tax_query'           => array(
            array(
              'taxonomy' => 'gallerys_type',
              'field'    => 'slug',
              'terms'    => 'services'
            )
          )
      );
      return $args;
      break;
  }
} 

默认情况下,$ filter_type的值为'all',并且显示所有帖子,但是当按下选项卡按钮时,值应该更改,帖子也必须更改。 如何实现?非常感谢!!!

0 个答案:

没有答案