简体   繁体   English

搜索页面上的WordPress has_posts()失败 - searchWP插件

[英]WordPress have_posts() failure on search page - searchWP plugin

I wanted to create a loop which displays search results in search.php file. 我想创建一个在search.php文件中显示搜索结果的循环。

I followed all of the instructions in the WordPress Codex tutorial located in here: 我按照位于此处的WordPress Codex教程中的所有说明进行操作:

https://codex.wordpress.org/The_Loop https://codex.wordpress.org/The_Loop

I have implemented the suggested code on my page like this: 我在我的页面上实现了建议的代码,如下所示:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

The problem is that the else condition fires (displaying 'Sorry, no posts matched your criteria.' ) suggesting that the have_posts() function failed. 问题是else条件触发(显示'抱歉,没有符合您标准的帖子。' )表明have_posts()函数失败。

IMPORTANT: I know that a lot of people in that situation suggest to use the WP_Query instance to get search results, and that does work, but the problem is I can't implement this suggestion. 重要提示:我知道很多人在这种情况下建议使用WP_Query实例来获取搜索结果,这确实有效,但问题是我无法实现这个建议。 That's because I am also using a search enhancing plugin. 那是因为我也在使用搜索增强插件。 It enhances relevance of search results and requires that no custom ( WP_Query ) calls are made, it allows only standard The Loop calls. 它增强了搜索结果的相关性,并且要求不进行自定义( WP_Query )调用,它只允许标准的The Loop调用。

That is why I need to fix the problem with have_posts() . 这就是我需要用have_posts()解决问题的have_posts()

Could anybody suggest me what might be the cause of have_posts() execution failiure? 任何人都可以告诉我一些可能导致have_posts()执行失败的原因吗?

UPDATE: As requested - I am adding the full page code below: 更新:根据要求 - 我在下面添加完整页面代码:

<?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(); ?>

Based on the discussion we had on chat we came to conclusion that the plugin the OP uses (searchWP) alters the query and returns no posts. 根据我们在聊天中的讨论,我们得出结论,OP使用的插件(searchWP)改变了查询并且没有返回任何帖子。

When the OP disabled the plugin everything worked as expected. 当OP禁用插件时,一切都按预期工作。

You have called get_header() twice and have called the_post() after header please remove it. 你已经调用了两次get_header()并在标题之后调用了the_post(),请将其删除。

Best Regards, Davit. 最诚挚的问候,戴维特。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM