WordPress主题开发教程 11_搜索

江河/ 06月11日/ WordPress/ 浏览 206

众里寻他千百度,蓦然回首,那人却在灯火阑珊处--辛弃疾


说起互联网公司的名字,百度-这个名字至少也能排进前三吧。和自己主要业务契合,又富有中国诗词浪漫气息。


大家好!今天我们聊一下 WordPress 主题中的搜索功能。



我们先看一下这个搜索表单的代码:


<form method="get" action="/">
        <input type="search" class="search-data" placeholder="输入搜索内容" value="" name="s" id="s" required="" style="color:black;">
        <button type="submit">搜索</button>
</form>


其实就是向站点发起一个GET请求,URL如下:


http://【你的域名】/?s=追格


像这种【搜索热词】的功能,其实就是一个 a 标签,url设置成 WordPress 搜索 URL 就可以了。




然后就会根据 WordPress 模板规则,寻找处理这个 URL 的模板 - search.php。


search.php 中主要就是一个 WordPress 主循环:


if (have_posts()) {
    while (have_posts()) {
        the_post(); // 在这里展示文章信息
    }
} else {
    echo '没有找到文章';
}


主要的代码如下:


<?php if (have_posts()) { ?>
  <div class="base-box">
  <!-- 文章列表 -->
  <?php
  while (have_posts()) {
      the_post();
  ?>
      <div class="post-div simple-item simple-left-side slide-in post-div-stick">
        <div class="simple-img simple-left-img">
          <a class="simple-left-img-a" href="<?php the_permalink() ?>" title="<?php the_title() ?>">
            <img alt="picture loss" src="<?php echo get_stylesheet_directory_uri() . '/images/jiangqie.png' ?>">
          </a>
          <?php $the_cat= get_the_category()[0] ?>
          <a class="simple-left-img-cat-a" href="<?php echo get_category_link($the_cat->cat_ID) ?>" title="<?php echo $the_cat->cat_name ?>">
            <strong><?php echo $the_cat->cat_name ?></strong>
          </a>
        </div>
        <div class="simple-content">
          <h2>
            <a href="<?php the_permalink() ?>" title="<?php the_title() ?>"><?php the_title() ?></a>
          </h2>
          <p><a href="<?php the_permalink() ?>" title="<?php the_excerpt() ?>"><?php the_excerpt() ?></a></p>
          <p class="simple-info">
            <a href="javascript:void(0)" title="酱茄小助理">
              <img alt="picture loss" src="<?php echo get_stylesheet_directory_uri() . '/images/default_avatar.jpg' ?>"><em>酱茄小助理</em>
            </a> · <cite>浏览 11356</cite> · <cite>点赞 38</cite> · <cite>评论 10</cite> ·
            <cite>1年前 (2022-10-11)</cite>
          </p>
        </div>
      </div>
  <?php
    }
  ?>
  </div>
<?php } else { ?>
  <div class="content-wrap">
    <div class="content-view mb-20">
        没有找到文章!
    </div>
  </div>
<?php } ?>


下载完整代码,可以去:


https://gitee.com/zhuige_com/course_jiangqie_theme


在主循环内部调用文章相关信息时,使用到的函数有 the_title,the_permalink,the_excerpt等。可以看出这些函数的规律,就是:the + 数据库表字段名。如果需要使用其他字段,也可以尝试一下,看有没有这个函数。


与之类似的,还有一组函数就是,get_the_title,get_the_permalink,get_the_excerpt等。查看他们的源代码可以发现:the_xxx ≈ echo get_the_xxx。也就是说只要获取,不需要显示的时候,可以使用 get_the_xxx。


定制搜索字段


WordPress 在搜索时默认是搜索标题,摘要,文章内容三个字段。之前曾被吐槽过好几次:为啥搜索出来的文章和关键词没关系?主要是因为一些人,以为搜索就是只对文章标题进行关键词匹配。


如果,我们希望只搜索标题包含关键字的文章,该怎么办呢?


可以使用 post_search_columns 过滤器设置。在functions.php中添加下面的代码即可:


function post_search_columns_callback($search_columns) {
    return ['post_title'];
}
add_filter('post_search_columns', 'post_search_columns_callback');


如果需要更复杂的定制,可以使用 posts_search 过滤器。这里就不啰嗦了,可以参考官方文档:


https://developer.wordpress.org/reference/hooks/posts_search/


调用 WP_Query 搜索文章


WP_Query 属于有点高级的内容了。在 WordPress 中,大多数查询文章的工作,都是由 WP_Query 完成的。一般的,都是 WordPress 自己调用 WP_Query 进行查询,我们只需要使用 WordPress 主循环显示查询结果就可以了。


使用 WP_Query 搜索文章,只需要:


$query = new WP_Query( array( 's' => 'keyword' ) );


之后,就使用我们熟悉的主循环搜索结果就可以了。


如果想搜索【包含关键词A,不包含关键词B】的文章,改如何怎么办呢?使用下面的代码就可以:


$query = new WP_Query( array( 's' => 'A -B' ) );


下面的代码也可以


http://【你的域名】/?s=A+-B


在搜索表单,输入关键词,也是支持这种模式的。需要注意的是中间有一个【空格】。A -B,完整的读出来是:A,空格,英文减号,B


WP_Query 的文档,值得花时间去看一下:https://developer.wordpress.org/reference/classes/wp_query/


这次的内容就这么多。我们下周见!



发表评论

暂无评论,抢个沙发...

客服 工单