让WordPress的搜索结果中包含自定义文章类型的内容

前端之家收集整理的这篇文章主要介绍了让WordPress的搜索结果中包含自定义文章类型的内容前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

使用wordpress建设网站的过程中,如果您的wordpress网站中添加自定义文章类型,我们想让wordpress搜索结果支持自定义文章类型,即可以搜索自定义文章类型中的内容。这篇wordpress教程中,我们就向大家介绍一下实现的方法

实现的方法非常简单,切换到主题目录,打开 functions.PHP 文件添加以下代码即可:

Crayon-5c891cbcd3d56722038301" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">functions.PHP
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="Copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-language">PHP
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891cbcd3d56722038301-1">1
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cbcd3d56722038301-2">2
Crayon-num" data-line="Crayon-5c891cbcd3d56722038301-3">3
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cbcd3d56722038301-4">4
Crayon-num" data-line="Crayon-5c891cbcd3d56722038301-5">5
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cbcd3d56722038301-6">6
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891cbcd3d56722038301-1">Crayon-c">//让搜索支持自定义文章类型
Crayon-line Crayon-striped-line" id="Crayon-5c891cbcd3d56722038301-2">Crayon-t">functionCrayon-h"> Crayon-e">searchAllCrayon-sy">(Crayon-h"> Crayon-v">$queryCrayon-h"> Crayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line" id="Crayon-5c891cbcd3d56722038301-3">Crayon-h">Crayon-st">ifCrayon-h"> Crayon-sy">(Crayon-h"> Crayon-v">$queryCrayon-i">is_searchCrayon-h"> Crayon-sy">)Crayon-h"> Crayon-sy">{Crayon-h"> Crayon-v">$queryCrayon-e">setCrayon-sy">(Crayon-h"> Crayon-s">'post_type'Crayon-sy">,Crayon-h"> Crayon-t">arrayCrayon-sy">(Crayon-h"> Crayon-s">'post'Crayon-sy">,Crayon-s">'books'Crayon-sy">,Crayon-h"> Crayon-s">'product'Crayon-sy">,Crayon-s">'works'Crayon-h"> Crayon-sy">)Crayon-sy">)Crayon-sy">;Crayon-h"> Crayon-sy">}Crayon-h">
Crayon-line Crayon-striped-line" id="Crayon-5c891cbcd3d56722038301-4">Crayon-h">Crayon-k ">returnCrayon-h"> Crayon-v">$queryCrayon-sy">;
Crayon-line" id="Crayon-5c891cbcd3d56722038301-5">Crayon-sy">}
Crayon-line Crayon-striped-line" id="Crayon-5c891cbcd3d56722038301-6">Crayon-e">add_filterCrayon-sy">(Crayon-h"> Crayon-s">'the_search_query'Crayon-sy">,Crayon-h"> Crayon-s">'searchAll'Crayon-h"> Crayon-sy">)Crayon-sy">;

请根据您自己的实际情况修改第 3 行数组(array)中的文章类型别名。

或者您也可以将下面的代码添加到当前主题的 functions.PHP 文件中:

Crayon-5c891cbcd3d5f831187667" class="Crayon-Syntax Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">functions.PHP
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="Copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-language">PHP
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891cbcd3d5f831187667-1">1
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cbcd3d5f831187667-2">2
Crayon-num" data-line="Crayon-5c891cbcd3d5f831187667-3">3
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cbcd3d5f831187667-4">4
Crayon-num" data-line="Crayon-5c891cbcd3d5f831187667-5">5
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cbcd3d5f831187667-6">6
Crayon-num" data-line="Crayon-5c891cbcd3d5f831187667-7">7
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cbcd3d5f831187667-8">8
Crayon-num" data-line="Crayon-5c891cbcd3d5f831187667-9">9
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cbcd3d5f831187667-10">10
Crayon-num" data-line="Crayon-5c891cbcd3d5f831187667-11">11
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cbcd3d5f831187667-12">12
Crayon-num" data-line="Crayon-5c891cbcd3d5f831187667-13">13
Crayon-num Crayon-striped-num" data-line="Crayon-5c891cbcd3d5f831187667-14">14
Crayon-num" data-line="Crayon-5c891cbcd3d5f831187667-15">15
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891cbcd3d5f831187667-1">Crayon-c">//让搜索支持所有自定义文章类型
Crayon-line Crayon-striped-line" id="Crayon-5c891cbcd3d5f831187667-2">Crayon-t">functionCrayon-h"> Crayon-e">include_post_types_in_searchCrayon-sy">(Crayon-v">$queryCrayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line" id="Crayon-5c891cbcd3d5f831187667-3">Crayon-h"> Crayon-st">ifCrayon-sy">(Crayon-e">is_searchCrayon-sy">(Crayon-sy">)Crayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line Crayon-striped-line" id="Crayon-5c891cbcd3d5f831187667-4">Crayon-h"> Crayon-v">$post_typesCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">get_post_typesCrayon-sy">(Crayon-t">arrayCrayon-sy">(Crayon-s">'public'Crayon-h"> Crayon-o">=Crayon-h"> Crayon-t">trueCrayon-sy">,Crayon-h"> Crayon-s">'exclude_from_search'Crayon-h"> Crayon-o">=Crayon-h"> Crayon-t">falseCrayon-sy">)Crayon-sy">,Crayon-h"> Crayon-s">'objects'Crayon-sy">)Crayon-sy">;
Crayon-line" id="Crayon-5c891cbcd3d5f831187667-5">Crayon-h"> Crayon-v">$searchable_typesCrayon-h"> Crayon-o">=Crayon-h"> Crayon-t">arrayCrayon-sy">(Crayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891cbcd3d5f831187667-6">Crayon-h"> Crayon-st">ifCrayon-sy">(Crayon-v">$post_typesCrayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line" id="Crayon-5c891cbcd3d5f831187667-7">Crayon-h"> Crayon-st">foreachCrayon-sy">(Crayon-h"> Crayon-v">$post_typesCrayon-h"> Crayon-st">asCrayon-h"> Crayon-v">$typeCrayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line Crayon-striped-line" id="Crayon-5c891cbcd3d5f831187667-8">Crayon-h"> Crayon-v">$searchable_typesCrayon-sy">[Crayon-sy">]Crayon-h"> Crayon-o">=Crayon-h"> Crayon-v">$typeCrayon-i">nameCrayon-sy">;
Crayon-line" id="Crayon-5c891cbcd3d5f831187667-9">Crayon-h"> Crayon-sy">}
Crayon-line Crayon-striped-line" id="Crayon-5c891cbcd3d5f831187667-10">Crayon-h"> Crayon-sy">}
Crayon-line" id="Crayon-5c891cbcd3d5f831187667-11">Crayon-h"> Crayon-v">$queryCrayon-e">setCrayon-sy">(Crayon-s">'post_type'Crayon-sy">,Crayon-h"> Crayon-v">$searchable_typesCrayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891cbcd3d5f831187667-12">Crayon-h"> Crayon-sy">}
Crayon-line" id="Crayon-5c891cbcd3d5f831187667-13">Crayon-h"> Crayon-k ">returnCrayon-h"> Crayon-v">$queryCrayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891cbcd3d5f831187667-14">Crayon-sy">}
Crayon-line" id="Crayon-5c891cbcd3d5f831187667-15">Crayon-e">add_actionCrayon-sy">(Crayon-s">'pre_get_posts'Crayon-sy">,Crayon-h"> Crayon-s">'include_post_types_in_search'Crayon-sy">)Crayon-sy">;

以上代码功能是让搜索结果支持所有自定义文章类型

原文链接:https://www.f2er.com/wordpress/238366.html

猜你在找的wordpress相关文章