按帖子类型对搜索结果进行分组,但隐藏没有结果的帖子类型

时间:2019-01-08 作者:Justin Picard

我在寻找一种干净的方法,按posttype对搜索结果进行分组。我目前有3种帖子类型:页面、帖子和词汇表。经过长时间的搜索this answer 这条线给了我我所需要的。

唯一的问题是,没有检查帖子类型是否没有搜索结果。如果一个帖子类型有0个结果,例如词汇表,它仍然显示帖子类型标题(以及帖子类型周围的容器)。我想要帖子类型li.search-results-post-type-item 被藏在那个箱子里。

我不是在找css/js黑客display: none; 解决方案我无法想象这不能用PHP完成。

提前感谢!

当前情况

Posts

<发布搜索结果1等

Pages

<页面搜索结果1、页面搜索结果2等

Glossary

(空)

理想情况

Posts

<发布搜索结果1等

Pages

  • 页面搜索结果1
  • 页面搜索结果2

    • 我的代码:

      <?php
          $search_query = new WP_Query(
          array(
              \'posts_per_page\'    => 10,
              \'s\'                 => esc_attr( $_POST[\'keyword\'] ),
              \'paged\'             => $paged,
              \'post_status\'       => \'publish\'
          )
      );
      
      if( $search_query->have_posts() ) : ?>
      
          <div class="search-suggestions-list-header">
              <?php echo $search_query->found_posts.\' results found\'; ?>
          </div>
      
          <ul class="search-results-list">
      
          <?php
              $types = array( \'post\', \'page\', \'glossary\' );
      
              foreach( $types as $type ) : ?>
      
                  <li class="search-results-post-type-item post-type-<?php echo $type ?>">
      
                      <header class="post-type-header">
                          <h5 class="post-type-title">
                              <?php
                                  $post_type_obj = get_post_type_object( $type );
                                  echo $post_type_obj->labels->name
                              ?>
                          </h5>
                      </header>
      
                      <ul class="search-results-list">
                          <?php
                          while( $search_query->have_posts() ): $search_query->the_post();
                              if( $type == get_post_type() ) : ?>
                                  <li class="search-results-list-item">
                                      <h4 class="entry-title"><?php the_title();?></h4>
                                  </li>
                              <?php
                              endif;
                          endwhile;
                          wp_reset_postdata();
                          ?>
                      </ul>
                  </li>
      
              <?php endforeach; ?>
      
          </ul>
      
      <?php else:
          echo \'<div class="search-suggestions-no-results">
                  <p>\' . __(\'Sorry, no results found\', \'text-domain\') . \'</p>
              </div>\';
      endif;
      

2 个回复
SO网友:nmr

要避免没有结果的类型,您可以。

再过一次循环,计算每种类型的出现次数,返回的结果数量不多(每页只有10个)

  • 当您遇到第一篇文章时,显示类型标题(移至内部while 循环,我知道,可读性较低的代码)
  • 遍历循环一次,然后将帖子标题收集到分为不同类型的表中
    <ul class="search-results-list">
        <?php
    
        $types = array( \'post\', \'page\', \'glossary\' );
        $occurrences = [];
        while( $search_query->have_posts() )
        {
            $search_query->next_post();
            $type = $search_query->post->post_type;
            if ( !isset($occurrences[$type]) )
                $occurrences[$type] = 1;
            else
                $occurrences[$type] += 1;
        }
        rewind_posts();
    
        foreach( $types as $type ) : 
    
            if ( !isset($occurrences[$type]) )
                continue;
            ?>
            <li class="search-results-post-type-item post-type-<?php echo $type ?>">
                //
                // remaining code
                //
            </li>
    
        <?php endforeach; ?>
    </ul>
    
    第二个选项
    $types = array( \'post\', \'page\', \'glossary\' );
    foreach( $types as $type ) : 
    
        $type_header_printed = false;
        ?>
        <?php
        while( $search_query->have_posts() ): 
            $search_query->the_post();
            if( $type == get_post_type() ) :
    
                // -- post type header -----
                if ( !$type_header_printed ) : 
    
                    $type_header_printed = true;
                    ?>
                    <li class="search-results-post-type-item post-type-<?php echo $type ?>">
                        <header class="post-type-header">
                            <h5 class="post-type-title">
                                <?php
                                    $post_type_obj = get_post_type_object( $type );
                                    echo $post_type_obj->labels->name
                                ?>
                            </h5>
                        </header>                    
                        <ul class="search-results-list">
    
                <?php // -- header end -----
                endif; ?>
    
                <li class="search-results-list-item">
                    <h4 class="entry-title"><?php the_title();?></h4>
                </li>
            <?php
    
            endif;
        endwhile;
        rewind_posts();
    
        if ( $type_header_printed ) : ?>
                </ul>
            </li>
        <?php endif; ?>
    
    <?php endforeach; ?>
    
    第三个选项
    <ul class="search-results-list">
        <?php
    
        $types = array( \'post\', \'page\', \'glossary\' );
        $posts_titles = [];
        while( $search_query->have_posts() )
        {
            $search_query->the_post();
            $type = $search_query->post->post_type;
            if ( !isset($posts_titles[$type]) )
                $posts_titles[$type] = [];
    
            $posts_titles[$type][] = get_the_title();
        }
        rewind_posts();
    
        foreach( $types as $type ) : 
    
            if ( !isset($posts_titles[$type]) )
                continue;
            ?>
            <li class="search-results-post-type-item post-type-<?php echo $type ?>">
                <header class="post-type-header">
                    <h5 class="post-type-title">
                        <?php
                            $post_type_obj = get_post_type_object( $type );
                            echo $post_type_obj->labels->name
                        ?>
                    </h5>
                </header>
                <ul class="search-results-list">
    
                <?php foreach( $posts_titles[$type] as $title ) : ?>
                    <li class="search-results-list-item">
                        <h4 class="entry-title"><?php echo htmlspecialchars($title); ?></h4>
                    </li>
                <?php endforeach; ?>
    
                </ul>
            </li>
    
        <?php endforeach; ?>
    </ul>
    

  • SO网友:Samuel Esteban

    我也有同样的问题。

    我的解决方案:

    $post_type 数组必须在WP_Query.

  • WP_Query 必须在foreach 声明

    <?php
        // Here, the Types
        $types = array( \'post\', \'page\', \'glossary\' );
    
        foreach( $types as $type ) :
    
        // Search Args better in a variable (for order purposes)
        $search_args = array(
                \'posts_per_page\'    => 10,
                \'s\'                 => esc_attr( $_POST[\'keyword\'] ),
                \'paged\'             => $paged,
                \'post_status\'       => \'publish\',
                \'post_type\'         => $type // This line was added
            );
    
        $search_query = new WP_Query($search_args);
    
        if( $search_query->have_posts() ) : ?>
    
        <div class="search-suggestions-list-header">
            <?php echo $search_query->found_posts.\' results found\'; ?>
        </div>
    
        <ul class="search-results-list">
    
            <li class="search-results-post-type-item post-type-<?php echo $type; ?>">
    
                <header class="post-type-header">
                    <h5 class="post-type-title">
                        <?php
                            $post_type_obj = get_post_type_object($type);
                            echo $post_type_obj->labels->name;
                        ?>
                    </h5>
                </header>
    
                <ul class="search-results-list">
                    <?php
                    while( $search_query->have_posts() ): $search_query->the_post();
                        if( $type == get_post_type() ) : ?>
                            <li class="search-results-list-item">
                                <h4 class="entry-title"><?php the_title();?></h4>
                            </li>
                        <?php
                        endif; // I think you\'ll need for style propouses
                    endwhile;
                    ?>
                </ul>
            </li>
    
        </ul>
    
    <?php else:
        echo \'<div class="search-suggestions-no-results">
                <p>\' . __(\'Sorry, no results found\', \'text-domain\') . \'</p>
            </div>\';
    endif;
    
    wp_reset_postdata(); // Here goes the reset
    
    endforeach; // End Foreach ?>
    
    希望有帮助。

  • 相关推荐

    Ordering terms whilst in loop

    我有一个页面模板,显示所有;“发布”;在两个自定义分类中,帖子显示在一个表中$type = $_GET[\'type\']; $category = $_GET[\'category\']; args = array( \'post-type\' => \'literature\', \'posts_per_page\' => -1, \'tax_query\' => array(