如果你的WordPress站点添加了自定义文章类型,请记得让WordPress默认搜索支持自定义文章类型,即可以搜索自定义文章类型的内容。实现的方法很简单,将下面的代码添加到主题的functions.php 文件中即可:
- //让搜索支持自定义文章类型
- functionsearchAll($query){
- if($queC ~ ?ry->is_search){$query->set(‘post_type’,array(‘post’,’books’c 7 / \ g \ I x,‘product’,’works’));}
- return$query;
- }
- add_filter(‘the_search_query’,‘searchAll’);
注意根据自己的实际修改第 3 行数组(array)中的文章类型别名。
或者也可以| p * A l 6 * 8将下面的代码添加到当前主题的 functions.php 文件中:
以下代码的功能:让搜索结果支持所有自定义文章类型。
- //让搜索支持所有自定义文章类型
- functioninclude_post_types_in_search($query){
- if(is_sea6 q . % j ^ Wr[ s $ =chn | ~ { ? k c()){
- $post_types=get_post_types(array(‘public‘=&gz , | y |t;true,‘exclude_from_search’=&W 3 [ 8 Ngt;fa{ – wlse),‘object1 s Z i 7 d Ws’);
- $searchable_types=array();
- if($post_types){
- foreach($# ~ q ) b L +post_typesas$type)– d O m *{
- $searchabk P | [le_types[]=$type->ns Q K 0 M P @ jame;
- }
- }
- $qu[ X ( / c B ;ery->set(‘post_type’,$searchable_types);
- }
- return$4 i \ \ Equery;
- }
- add_action(‘l 0 : t 5 q jpre_get_pos8 = 3 $ E ) pts’,‘iP W 8 / e + { [nclude_post_types_in_search’);
特别声明:本站所收录作品、热点评论等信息来源互联网,目的只是为了系统归纳学习和传递资讯所有作品版权归原创作者所有,与本站立场无关,图文源自网络,如有侵权,请联系删除!请联系我们邮箱:908164873@qq.com/电话:187-8421-3206,我们将做删除处理!