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

问题描述 投票:7回答:4

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

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

知道如何让主题同时搜索文章和页面吗?

这里是大部分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; ?>
wordpress search themes
4个回答
0
投票

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


35
投票

将此代码添加到您的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


0
投票

WP搜索http://wpsear.ch/具有此功能。您可以调整要在结果页面中显示的帖子类型。


0
投票

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

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

要插入的代码:]

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

因此,您的代码应如下所示:

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

让我知道它是否有效。

© www.soinside.com 2019 - 2024. All rights reserved.