【问题标题】:Wordpress - how to search pages onlyWordpress - 如何仅搜索页面
【发布时间】:2011-05-16 14:40:51
【问题描述】:

我需要一些关于 Wordpress 搜索的帮助。我想知道的是如何只搜索 Wordpress 页面。

【问题讨论】:

    标签: wordpress search


    【解决方案1】:

    您可能想在 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');
    

    【讨论】:

      【解决方案2】:

      在您的 search.php 中,找到 The 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; ?>
      

      让我知道它是否有效。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-02
        • 1970-01-01
        • 2012-12-11
        • 1970-01-01
        • 1970-01-01
        • 2014-01-10
        • 1970-01-01
        • 2017-11-18
        相关资源
        最近更新 更多