WordPress 搜索仅适用于帖子,不适用于页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5147297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:43:42  来源:igfitidea点击:

WordPress search only works with posts, not pages

wordpresssearchthemes

提问by Ryan

I'm working on my own custom WordPress theme, using a blank, default template to start. I haven't edited the search.php file.

我正在开发我自己的自定义 WordPress 主题,使用空白的默认模板开始。我还没有编辑 search.php 文件。

Many of my WordPress posts are Pages. Unfortunately, the site search only retrieves posts, not pages.

我的许多 WordPress 帖子都是页面。不幸的是,站点搜索只检索帖子,而不是页面。

Any idea how to get the theme to search both posts and pages?

知道如何让主题搜索帖子和页面吗?

Here is the majority of search.php:

这是search.php的大部分内容:

<?php if (have_posts()) : ?>

    <h3>Search Results</h3>

    <?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>

    <?php while (have_posts()) : the_post(); ?>

        <div <?php post_class() ?> id="post-<?php the_ID(); ?>">

            <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>

            <?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>

            <div class="entry">
                <?php the_excerpt(); ?>
            </div>

        </div>

    <?php endwhile; ?>

    <?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>

<?php else : ?>

    <h3>No posts found.</h3>

<?php endif; ?>

采纳答案by markratledge

Lack of page search and search results ranked by post date instead of relevance is a typical WP problem. Try http://wordpress.org/extend/plugins/relevanssi/

缺乏页面搜索和按发布日期而不是相关性排名的搜索结果是典型的 WP 问题。试试http://wordpress.org/extend/plugins/relevanssi/

回答by mark

Add this code to your functions.php file.

将此代码添加到您的functions.php 文件中。

function wpshock_search_filter( $query ) {
    if ( $query->is_search ) {
        $query->set( 'post_type', array('post','page') );
    }
    return $query;
}
add_filter('pre_get_posts','wpshock_search_filter');

http://wpth.net/limit-wordpress-search-results-to-specific-post-types

http://wpth.net/limit-wordpress-search-results-to-specific-post-types

回答by Hmayak Tigranyan

WP Search http://wpsear.ch/has that ability.You can adjust what post types you want to show in results page.

WP Search http://wpsear.ch/具有这种能力。您可以调整要在结果页面中显示的帖子类型。

回答by Rajeev

In your search.php, find The Loop and insert this code just after it. You can recognize the Loop because it usually starts with:

在您的 search.php 中,找到 The Loop 并在其后面插入此代码。您可以识别循环,因为它通常以以下开头:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>

Code to be inserted:

要插入的代码:

if (is_search() && ($post->post_type=='page')) continue; 

So, your code should be like this:

所以,你的代码应该是这样的:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<?php if (is_search() && ($post->post_type=='page')) continue; ?>

Let me know if it worked.

让我知道它是否有效。