【问题标题】:wordpress sidebar loops wont work when in search.php when no results found在 search.php 中找不到结果时,wordpress 侧边栏循环将不起作用
【发布时间】:2012-12-28 09:48:40
【问题描述】:

在 wordpress 中的 search.php 我有这个..

get_header(); ?>
...html/css
 <?php if ( have_posts() ) : ?>   
 <?php while (have_posts()) : the_post(); 
 ...loop stuff
 <?php endwhile; ?>
 <?php wp_pagenavi(); ?>
 <?php else : ?>
 <h3><?php _e( 'Sorry, but nothing matched your search criteria. Please try again with some different keywords.', 'hartyinternational' ); ?></h3>
                    <div class="nothing-found"><?php get_search_form(); ?></div>
    <?php endif; ?>
   <?php wp_reset_query(); ?>

然后我用WP_Query 循环构建了自己的侧边栏, 例如

    if ( have_posts() ) :
$counter = 0;

            $the_query =  new WP_Query( array( 'posts_per_page' => 5, 'cat' => 4 ) );
            while ($the_query->have_posts() ) : $the_query->the_post(); 
            $counter++;
            if ( $counter == 1 || $counter == 3 || $counter == 5   ){
            ?>
            <a href="<?php the_permalink(); ?>"><div class="recent-position-single"><?php the_title();  ?></div> </a>
        <?php }
            else {
            ?> <a href="<?php the_permalink(); ?>"><div class="recent-position-single darker-shade"><?php the_title();  ?></div> </a>
        <?php }



            endwhile;
 wp_reset_query();
 endif; 
 wp_reset_postdata(); ?>

还有一个类似的, 当找到搜索结果时,这些工作完全正常,但是当没有找到结果时,循环无法加载任何内容,有什么建议吗?

这似乎不是循环中存在循环或类似情况的情况,我似乎无法弄清楚可能出了什么问题,任何帮助将非常感谢。

【问题讨论】:

  • 边栏是否加载,只是不加载任何查询?还是根本不加载?您是否在侧边栏中添加了一些额外的内容以确保其加载到您的空搜索结果页面上?
  • 您好,我删除了if ( have_posts() ):,这似乎解决了问题,但感谢您的回复。
  • 很高兴你能成功。

标签: php wordpress search


【解决方案1】:

我从查询中删除了if ( have_posts() ):,这解决了问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多