我想创建一个循环,在搜索中显示搜索结果。php文件。
我遵循了WordPress Codex教程中的所有说明,该教程位于:
https://codex.wordpress.org/The_Loop
我在页面上实现了建议的代码,如下所示:<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; else: ?>
<p><?php _e(\'Sorry, no posts matched your criteria.\'); ?></p>
<?php endif; ?>
问题是else
条件触发(显示“抱歉,没有帖子符合您的条件。”)表明have_posts()
函数失败或未返回帖子,但我确信我在搜索框中键入的内容应该会返回大量结果。IMPORTANT: 我知道很多人在这种情况下建议使用WP_Query
实例来获取搜索结果,这确实有效,但问题是我无法实现此建议。这是因为我还使用了一个搜索增强插件。它增强了搜索结果的相关性,并要求no 自定义(WP_Query
) 调用,它只允许标准的循环调用。
这就是为什么我需要用have_posts()
.
Could anybody suggest me what might be the cause of have_posts()
execution failiure?
<?php
get_header();
the_post();
?>
<?php define(\'WP_USE_THEMES\', false); get_header(); ?>
<div class="iecontent">
<div class="g960">
<div id="search">
<div id="search-box">
<form action="<?php bloginfo( \'url\' ); ?>" method="get">
<div>
<input type="text" name="s" value="<?php echo get_search_query()?>" placeholder="Search..."/>
<input type="submit" name="searchsubmit" value="" class="submit"/>
</div>
</form>
</div>
<div id="search-results">
<div class="search-result">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; else: ?>
<p><?php _e(\'Sorry, no posts matched your criteria.\'); ?></p>
<?php endif; ?>
</div>
</div>
<?php wpbeginner_numeric_posts_nav(); ?>
<div class="clearfix"></div>
</div>
</div>
</div>
<?php get_footer(); ?>