Wordpress-如何仅搜索页面

问题描述 投票:1回答:2

我需要有关Wordpress搜索的帮助。我想知道的是如何仅搜索Wordpress页面。

wordpress search
2个回答
4
投票

您可能想在wordpress论坛上查看此page。您必须在搜索表单中添加一个隐藏的表单字段,并添加一个挂钩以更新wordpress生成的查询。

编辑:这是上述论坛文章中的代码。

在搜索表中:

<input type="hidden" name="post_type" value="page" />

在functions.php中:

function mySearchFilter($query) {
    $post_type = $_GET['type'];
    if (!$post_type) {
        $post_type = 'any';
    }
    if ($query->is_search) {
        $query->set('post_type', $post_type);
    };
    return $query;
};

add_filter('pre_get_posts','mySearchFilter');

0
投票

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

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

要插入的代码:]

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

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

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

让我知道它是否有效。

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