【问题标题】:rewind_posts() and displaying 'no results found' inforewind_posts() 并显示“未找到结果”信息
【发布时间】:2021-12-19 00:24:50
【问题描述】:

我正在使用 rewind_posts() 函数根据帖子类型对搜索结果进行分组,并将它们显示在相关选项卡下。循环工作得很好,但我正在努力显示no results found 信息。

例如,如果我正在搜索文档,我希望帖子显示在documents 下,但在pagepeople 选项卡下我需要回显No results

        <?php
      if ( have_posts() ) { ?>
        <section class="tabs" id="pages">
          <?php while( have_posts() ) { the_post(); ?>
          <?php if ( $post->post_type == 'page' ) { 
           include(locate_template('partials/pages.php', false, false));  ?>
          <?php } ?>
          <?php
        } ?>
        </section>
        <?php
        rewind_posts(); ?>
          <section class="tabs" id="people">
            <?php while( have_posts() ) { the_post(); ?>
            <?php if ( $post->post_type == 'people' ) { 
             include(locate_template('partials/profile.php', false, false));  ?>
            <?php } ?>
            <?php
          } ?>
          </section>
        <?php
        rewind_posts(); ?>
        <section class="tabs" id="documents">
          <?php while( have_posts() ) { the_post(); ?>
          <?php if ( $post->post_type == 'documents' ) { 
           include(locate_template('partials/document.php', false, false));  ?>
          <?php } ?>
          <?php
        } ?>
        </section>
        <?php
        rewind_posts(); ?>
      </div>

【问题讨论】:

    标签: php wordpress loops


    【解决方案1】:

    看起来你正在做的事情可以通过使用开关或 if/else 而不是使用rewind_posts()更有效

    这应该可以在一个循环中运行,尽管我实际上无法测试您的代码。

    if ( have_posts() ) : ?>
    <section class="tabs" id="<?php echo esc_attr( $post->post_type ); ?>">
        <?php while ( have_posts() ) {
        the_post(); ?>
        <?php 
         // Determine the post type and load the appropriate template
         switch ( $post->post_type ) {
            case 'page':
            default:
                include( locate_template( 'partials/pages.php', false, false ) );
                break;
            case 'people':
                include( locate_template( 'partials/profile.php', false, false ) );
                break;
            case 'documents':
                include( locate_template( 'partials/document.php', false, false ) );
                break;
            } 
        } // endwhile
        ?>
    </section>
    <?php else: 
        echo 'No Posts Found';
    endif;
    

    【讨论】:

    • 感谢您的回复。不过,您的代码与我的代码完全相同。只有在所有帖子类型中都没有帖子时,我才会收到“未找到帖子”。我只想在没有结果的选项卡下显示此信息。
    猜你喜欢
    • 1970-01-01
    • 2012-05-31
    • 2013-09-24
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多