{"id":32850,"date":"2024-11-25T08:34:12","date_gmt":"2024-11-25T00:34:12","guid":{"rendered":"https:\/\/fwq.ai\/blog\/32850\/"},"modified":"2024-11-25T08:34:12","modified_gmt":"2024-11-25T00:34:12","slug":"%e5%a6%82%e4%bd%95%e5%88%9b%e5%bb%ba%e4%b8%80%e4%b8%aa%e8%87%aa%e5%ae%9a%e4%b9%89%e7%9a%84wordpress%e6%90%9c%e7%b4%a2%e6%8f%92%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/32850\/","title":{"rendered":"\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684WordPress\u641c\u7d22\u63d2\u4ef6"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169390595019573.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684WordPress\u641c\u7d22\u63d2\u4ef6\u63d2\u56fe\" alt=\"\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684WordPress\u641c\u7d22\u63d2\u4ef6\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684WordPress\u641c\u7d22\u63d2\u4ef6<\/p>\n<p>WordPress\u4f5c\u4e3a\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u5185\u5bb9\u7ba1\u7406\u7cfb\u7edf\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u5f3a\u5927\u7684\u9ed8\u8ba4\u529f\u80fd\uff0c\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f\u641c\u7d22\u529f\u80fd\u3002\u7136\u800c\uff0c\u6709\u65f6\u5019\u9ed8\u8ba4\u7684\u641c\u7d22\u529f\u80fd\u5e76\u4e0d\u80fd\u5b8c\u5168\u6ee1\u8db3\u6211\u4eec\u7684\u9700\u6c42\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684WordPress\u641c\u7d22\u63d2\u4ef6\u6765\u5b9e\u73b0\u6211\u4eec\u6240\u9700\u7684\u641c\u7d22\u529f\u80fd\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684\u81ea\u5b9a\u4e49\u641c\u7d22\u63d2\u4ef6\uff0c\u5e76\u7ed9\u51fa\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u7b2c\u4e00\u6b65\uff1a\u521b\u5efa\u63d2\u4ef6\u6587\u4ef6\u5939<\/p>\n<p>\u9996\u5148\uff0c\u5728WordPress\u7684\u63d2\u4ef6\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u5939\uff0c\u7528\u4e8e\u5b58\u653e\u6211\u4eec\u7684\u81ea\u5b9a\u4e49\u641c\u7d22\u63d2\u4ef6\u3002\u53ef\u4ee5\u5c06\u6587\u4ef6\u5939\u547d\u540d\u4e3a&#8221;custom-search&#8221;\u6216\u8005\u5176\u4ed6\u4f60\u559c\u6b22\u7684\u540d\u79f0\u3002<\/p>\n<p>\u7b2c\u4e8c\u6b65\uff1a\u521b\u5efa\u63d2\u4ef6\u4e3b\u6587\u4ef6<\/p>\n<p>\u5728\u63d2\u4ef6\u6587\u4ef6\u5939\u4e2d\u521b\u5efa\u4e00\u4e2a\u4e3b\u6587\u4ef6\uff0c\u547d\u540d\u4e3a&#8221;custom-search.php&#8221;\u3002\u8fd9\u662f\u63d2\u4ef6\u7684\u5165\u53e3\u70b9\uff0c\u5305\u62ec\u5b9a\u4e49\u63d2\u4ef6\u7684\u57fa\u672c\u4fe1\u606f\u548c\u542f\u7528\u63d2\u4ef6\u6240\u9700\u7684\u51fd\u6570\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f&#8221;custom-search.php&#8221;\u7684\u57fa\u672c\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>&lt;?php \/*\nPlugin Name: Custom Search\nPlugin URI: https:\/\/www.example.com\nDescription: This is a custom search plugin for WordPress.\nVersion: 1.0\nAuthor: Your Name\nAuthor URI: https:\/\/www.example.com\n*\/\n\n\/\/ \u5728\u8fd9\u91cc\u6dfb\u52a0\u63d2\u4ef6\u529f\u80fd\u4ee3\u7801\n?&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u63d0\u4f9b\u63d2\u4ef6\u7684\u57fa\u672c\u4fe1\u606f\uff0c\u5305\u62ec\u63d2\u4ef6\u7684\u540d\u79f0\u3001\u63cf\u8ff0\u3001\u7248\u672c\u53f7\u548c\u4f5c\u8005\u7b49\u3002\u63d2\u4ef6\u7684\u529f\u80fd\u4ee3\u7801\u5c06\u5728\u8fd9\u4e2a\u6587\u4ef6\u7684\u5e95\u90e8\u6dfb\u52a0\u3002<\/p>\n<p>\u7b2c\u4e09\u6b65\uff1a\u6dfb\u52a0\u641c\u7d22\u8868\u5355<\/p>\n<p>\u5728\u63d2\u4ef6\u7684\u4e3b\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u641c\u7d22\u8868\u5355\uff0c\u7528\u4e8e\u8f93\u5165\u641c\u7d22\u5173\u952e\u5b57\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u6dfb\u52a0\u641c\u7d22\u8868\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>function custom_search_form() {\n    $form = '\n        <\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236       &#8216;; return $form; } <\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86home_url(&#8216;\/&#8217;)\u51fd\u6570\u6765\u83b7\u53d6\u7f51\u7ad9\u7684\u9996\u9875URL\uff0c\u5c06\u5176\u4f5c\u4e3a\u641c\u7d22\u8868\u5355\u7684action\u5c5e\u6027\u3002get_search_query()\u51fd\u6570\u8fd4\u56de\u5f53\u524d\u641c\u7d22\u5173\u952e\u5b57\uff0c\u7528\u4e8e\u5c06\u5176\u586b\u5145\u5230\u641c\u7d22\u8868\u5355\u4e2d\u3002<\/p>\n<p>\u7b2c\u56db\u6b65\uff1a\u5904\u7406\u641c\u7d22\u8bf7\u6c42<\/p>\n<p>\u5728\u63d2\u4ef6\u7684\u4e3b\u6587\u4ef6\u4e2d\u6dfb\u52a0\u5904\u7406\u641c\u7d22\u8bf7\u6c42\u7684\u51fd\u6570\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u5904\u7406\u641c\u7d22\u8bf7\u6c42\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>function custom_search_results() {\n    $keyword = isset($_GET['s']) ? $_GET['s'] : '';\n     \n    if ($keyword) {\n        $search_results = new WP_Query(array(\n            's' =&amp;gt; $keyword,\n            'post_type' =&amp;gt; 'post',\n            'order' =&amp;gt; 'DESC',\n            'orderby' =&amp;gt; 'date',\n        ));\n         \n        if ($search_results-&amp;gt;have_posts()) {\n            while ($search_results-&amp;gt;have_posts()) {\n                $search_results-&amp;gt;the_post();\n                 \n                \/\/ \u5728\u8fd9\u91cc\u8f93\u51fa\u641c\u7d22\u7ed3\u679c\u7684HTML\u4ee3\u7801\n            }\n        } else {\n            echo 'No results found';\n        }\n         \n        wp_reset_postdata();\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u901a\u8fc7$_GET[&#8216;s&#8217;]\u83b7\u53d6\u5230\u641c\u7d22\u5173\u952e\u5b57\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528WP_Query\u7c7b\u6765\u83b7\u53d6\u76f8\u5173\u7684\u641c\u7d22\u7ed3\u679c\u3002\u901a\u8fc7\u5728WP_Query\u5bf9\u8c61\u7684\u914d\u7f6e\u4e2d\u8bbe\u7f6es\u53c2\u6570\u4e3a\u641c\u7d22\u5173\u952e\u5b57\uff0cpost_type\u53c2\u6570\u4e3a&#8221;post&#8221;\uff0c\u6211\u4eec\u53ef\u4ee5\u53ea\u641c\u7d22\u6587\u7ae0\u7c7b\u578b\u7684\u5185\u5bb9\u3002<\/p>\n<p>\u5728\u5faa\u73af\u904d\u5386\u641c\u7d22\u7ed3\u679c\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528have_posts()\u51fd\u6570\u6765\u5224\u65ad\u662f\u5426\u6709\u641c\u7d22\u7ed3\u679c\u3002\u5982\u679c\u6709\u641c\u7d22\u7ed3\u679c\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528the_post()\u51fd\u6570\u6765\u51c6\u5907\u4e0b\u4e00\u4e2a\u641c\u7d22\u7ed3\u679c\u8fdb\u884c\u8f93\u51fa\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u8981\u81ea\u5b9a\u4e49\u641c\u7d22\u7ed3\u679c\u7684HTML\u4ee3\u7801\u3002<\/p>\n<p>\u7b2c\u4e94\u6b65\uff1a\u6ce8\u518c\u63d2\u4ef6\u7684\u77ed\u4ee3\u7801<\/p>\n<p>\u5728\u63d2\u4ef6\u7684\u4e3b\u6587\u4ef6\u4e2d\u6ce8\u518c\u4e00\u4e2a\u77ed\u4ee3\u7801\uff0c\u7528\u4e8e\u5728\u7f51\u7ad9\u4e2d\u6dfb\u52a0\u641c\u7d22\u8868\u5355\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u6ce8\u518c\u77ed\u4ee3\u7801\u7684\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre>function custom_search_shortcode() {\n    ob_start();\n    custom_search_form();\n    return ob_get_clean();\n}\nadd_shortcode('custom_search', 'custom_search_shortcode');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86ob_start()\u548cob_get_clean()\u6765\u6355\u83b7\u641c\u7d22\u8868\u5355\u7684\u8f93\u51fa\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u77ed\u4ee3\u7801\u7684\u8fd4\u56de\u503c\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528add_shortcode()\u51fd\u6570\u6765\u6ce8\u518c\u77ed\u4ee3\u7801\uff0c\u5e76\u5c06\u81ea\u5b9a\u4e49\u641c\u7d22\u8868\u5355\u7684\u51fd\u6570\u4f5c\u4e3a\u56de\u8c03\u51fd\u6570\u3002<\/p>\n<p>\u7b2c\u516d\u6b65\uff1a\u542f\u7528\u63d2\u4ef6<\/p>\n<p>\u4fdd\u5b58\u63d2\u4ef6\u7684\u4e3b\u6587\u4ef6\uff0c\u5e76\u901a\u8fc7WordPress\u7684\u540e\u53f0\u7ba1\u7406\u7cfb\u7edf\u5c06\u63d2\u4ef6\u542f\u7528\u3002\u4e4b\u540e\uff0c\u4f60\u53ef\u4ee5\u5728\u4efb\u4f55\u5e0c\u671b\u6dfb\u52a0\u641c\u7d22\u8868\u5355\u7684\u4f4d\u7f6e\u4f7f\u7528[custom_search]\u77ed\u4ee3\u7801\u6765\u5c55\u793a\u641c\u7d22\u8868\u5355\u3002<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684WordPress\u641c\u7d22\u63d2\u4ef6\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u6211\u4eec\u6240\u9700\u7684\u641c\u7d22\u529f\u80fd\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684\u81ea\u5b9a\u4e49\u641c\u7d22\u63d2\u4ef6\uff0c\u5e76\u7ed9\u51fa\u4e86\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u548c\u6280\u672f\u6c34\u5e73\u8fdb\u4e00\u6b65\u6269\u5c55\u548c\u6539\u8fdb\u63d2\u4ef6\u7684\u529f\u80fd\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5e2e\u52a9\u4f60\u521b\u5efa\u4e00\u4e2a\u6ee1\u8db3\u9700\u6c42\u7684\u81ea\u5b9a\u4e49\u641c\u7d22\u63d2\u4ef6\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684WordPress\u641c\u7d22\u63d2\u4ef6\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684WordPress\u641c\u7d22\u63d2\u4ef6 WordPress\u4f5c\u4e3a\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u5185\u5bb9\u7ba1\u7406\u7cfb\u7edf\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u5f3a\u5927\u7684\u9ed8\u8ba4\u529f\u80fd\uff0c\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f\u641c\u7d22\u529f\u80fd\u3002\u7136\u800c\uff0c\u6709\u65f6\u5019\u9ed8\u8ba4\u7684\u641c\u7d22\u529f\u80fd\u5e76\u4e0d\u80fd\u5b8c\u5168\u6ee1\u8db3\u6211\u4eec\u7684\u9700\u6c42\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684WordPress\u641c\u7d22\u63d2\u4ef6\u6765\u5b9e\u73b0\u6211\u4eec\u6240\u9700\u7684\u641c\u7d22\u529f\u80fd\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684\u81ea\u5b9a\u4e49\u641c\u7d22\u63d2\u4ef6\uff0c\u5e76\u7ed9\u51fa\u76f8\u5e94\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u7b2c\u4e00\u6b65\uff1a\u521b\u5efa\u63d2\u4ef6\u6587\u4ef6\u5939 \u9996\u5148\uff0c\u5728WordPress\u7684\u63d2\u4ef6\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u5939\uff0c\u7528\u4e8e\u5b58\u653e\u6211\u4eec\u7684\u81ea\u5b9a\u4e49\u641c\u7d22\u63d2\u4ef6\u3002\u53ef\u4ee5\u5c06\u6587\u4ef6\u5939\u547d\u540d\u4e3a&#8221;custom-search&#8221;\u6216\u8005\u5176\u4ed6\u4f60\u559c\u6b22\u7684\u540d\u79f0\u3002 \u7b2c\u4e8c\u6b65\uff1a\u521b\u5efa\u63d2\u4ef6\u4e3b\u6587\u4ef6 \u5728\u63d2\u4ef6\u6587\u4ef6\u5939\u4e2d\u521b\u5efa\u4e00\u4e2a\u4e3b\u6587\u4ef6\uff0c\u547d\u540d\u4e3a&#8221;custom-search.php&#8221;\u3002\u8fd9\u662f\u63d2\u4ef6\u7684\u5165\u53e3\u70b9\uff0c\u5305\u62ec\u5b9a\u4e49\u63d2\u4ef6\u7684\u57fa\u672c\u4fe1\u606f\u548c\u542f\u7528\u63d2\u4ef6\u6240\u9700\u7684\u51fd\u6570\u3002 \u4ee5\u4e0b\u662f&#8221;custom-search.php&#8221;\u7684\u57fa\u672c\u4ee3\u7801\u793a\u4f8b\uff1a &lt;?php \/* Plugin Name: Custom Search Plugin URI: https:\/\/www.example.com Description: This is a custom search plugin for WordPress. Version: 1.0 Author: Your Name Author URI: https:\/\/www.example.com *\/ \/\/ \u5728\u8fd9\u91cc\u6dfb\u52a0\u63d2\u4ef6\u529f\u80fd\u4ee3\u7801 ?&gt; \u767b\u5f55\u540e\u590d\u5236 \u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u63d0\u4f9b\u63d2\u4ef6\u7684\u57fa\u672c\u4fe1\u606f\uff0c\u5305\u62ec\u63d2\u4ef6\u7684\u540d\u79f0\u3001\u63cf\u8ff0\u3001\u7248\u672c\u53f7\u548c\u4f5c\u8005\u7b49\u3002\u63d2\u4ef6\u7684\u529f\u80fd\u4ee3\u7801\u5c06\u5728\u8fd9\u4e2a\u6587\u4ef6\u7684\u5e95\u90e8\u6dfb\u52a0\u3002 \u7b2c\u4e09\u6b65\uff1a\u6dfb\u52a0\u641c\u7d22\u8868\u5355 \u5728\u63d2\u4ef6\u7684\u4e3b\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u641c\u7d22\u8868\u5355\uff0c\u7528\u4e8e\u8f93\u5165\u641c\u7d22\u5173\u952e\u5b57\u3002 \u4ee5\u4e0b\u662f\u6dfb\u52a0\u641c\u7d22\u8868\u5355\u7684\u4ee3\u7801\u793a\u4f8b\uff1a function custom_search_form() { $form = &#8216; \u767b\u5f55\u540e\u590d\u5236 &#8216;; return $form; } \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86home_url(&#8216;\/&#8217;)\u51fd\u6570\u6765\u83b7\u53d6\u7f51\u7ad9\u7684\u9996\u9875URL\uff0c\u5c06\u5176\u4f5c\u4e3a\u641c\u7d22\u8868\u5355\u7684action\u5c5e\u6027\u3002get_search_query()\u51fd\u6570\u8fd4\u56de\u5f53\u524d\u641c\u7d22\u5173\u952e\u5b57\uff0c\u7528\u4e8e\u5c06\u5176\u586b\u5145\u5230\u641c\u7d22\u8868\u5355\u4e2d\u3002 \u7b2c\u56db\u6b65\uff1a\u5904\u7406\u641c\u7d22\u8bf7\u6c42 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-32850","post","type-post","status-publish","format-standard","hentry","category-cms"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32850","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=32850"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32850\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=32850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=32850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=32850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}