{"id":29485,"date":"2024-11-25T09:16:24","date_gmt":"2024-11-25T01:16:24","guid":{"rendered":"https:\/\/fwq.ai\/blog\/29485\/"},"modified":"2024-11-25T09:16:24","modified_gmt":"2024-11-25T01:16:24","slug":"%e5%a6%82%e4%bd%95%e4%b8%bawordpress%e5%88%86%e7%b1%bb%e6%b7%bb%e5%8a%a0%e9%80%89%e6%8b%a9%e4%b8%8d%e5%90%8c%e6%a8%a1%e6%9d%bf%e9%80%89%e9%a1%b9","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/29485\/","title":{"rendered":"\u5982\u4f55\u4e3aWordPress\u5206\u7c7b\u6dfb\u52a0\u9009\u62e9\u4e0d\u540c\u6a21\u677f\u9009\u9879"},"content":{"rendered":"<p>\u4e0b\u9762\u7531\/&#8221; target=&#8221;_blank&#8221;&gt;<strong>wordpress\u6559\u7a0b<\/strong>\u680f\u76ee\u7ed9\u5927\u5bb6\u4e3awordpress\u5206\u7c7b\u6dfb\u52a0\u9009\u62e9\u4e0d\u540c\u6a21\u677f\u9009\u9879\u7684\u65b9\u6cd5\uff0c\u5e0c\u671b\u5bf9\u9700\u8981\u7684\u670b\u53cb\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/020\/5dcb7ef02721a746.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4e3aWordPress\u5206\u7c7b\u6dfb\u52a0\u9009\u62e9\u4e0d\u540c\u6a21\u677f\u9009\u9879\u63d2\u56fe\" alt=\"\u5982\u4f55\u4e3aWordPress\u5206\u7c7b\u6dfb\u52a0\u9009\u62e9\u4e0d\u540c\u6a21\u677f\u9009\u9879\u63d2\u56fe\" \/><\/p>\n<p>\u6211\u4eec\u6709\u65f6\u4f1a\u6839\u636e\u5206\u7c7b\u7684\u5185\u5bb9\uff0c\u60f3\u8ba9\u4e0d\u540c\u7684\u5206\u7c7b\u4ee5\u4e0d\u540c\u7684\u6837\u5f0f\u5c55\u793a\u3002\u901a\u5e38\u7684\u65b9\u6cd5\u662f\u5728\u5f53\u524d\u4e3b\u9898\u6839\u76ee\u5f55\u4e2d\u591a\u5efa\u51e0\u4e2a\u4e0d\u540c\u5e03\u5c40\u6837\u5f0f\u7684\u5206\u7c7b\u6a21\u677f\uff0c\u6bd4\u5982category-1.php\u3001category-2.php\u3001category-3.php&#8230;..\uff0c\u540e\u9762\u7684\u6570\u5b57\u662f\u5bf9\u5e94\u8be5\u7684\u5206\u7c7bID\u53f7\uff0c\u6216\u8005\u4f7f\u7528is_category()\u51fd\u6570\u6dfb\u52a0\u5224\u65ad\uff0c\u64cd\u4f5c\u6709\u4e9b\u7e41\u7410\u3002\u6709\u4e2a\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u5b89\u88c5 Custom Category Templates \u63d2\u4ef6\u3002<\/p>\n<p>\u542f\u7528\u63d2\u4ef6\u540e\uff0c\u5728\u7f16\u8f91\u5206\u7c7b\u65f6\u4f1a\u6dfb\u52a0\u4e00\u4e2a\u9009\u62e9\u6a21\u677f\u7684\u9009\u9879\u3002<\/p>\n<p>\u5236\u4f5c\u51e0\u4e2a\u4e0d\u540c\u5e03\u5c40\u98ce\u683c\u7684\u9875\u9762\u6a21\u677f\uff0c\u6a21\u677f\u5934\u90e8\u5fc5\u987b\u6709\u7c7b\u4f3c\u7684\u6807\u8bc6\uff1a<\/p>\n<pre>&lt;?php \/*\nTemplate Name: \u6a21\u677fA\n*\/<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u540e\u5728\u7f16\u8f91\u6216\u8005\u6dfb\u52a0\u5206\u7c7b\u65f6\uff0c\u4e3a\u4e0d\u540c\u7684\u5206\u7c7b\u9009\u62e9\u4e13\u7528\u7684\u6a21\u677f\u5373\u53ef\u3002<\/p>\n<p>\u6548\u679c\u5982\u56fe\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/image\/391\/296\/810\/1573617313450978.png\" class=\"aligncenter\" title=\"\u5982\u4f55\u4e3aWordPress\u5206\u7c7b\u6dfb\u52a0\u9009\u62e9\u4e0d\u540c\u6a21\u677f\u9009\u9879\u63d2\u56fe1\" alt=\"\u5982\u4f55\u4e3aWordPress\u5206\u7c7b\u6dfb\u52a0\u9009\u62e9\u4e0d\u540c\u6a21\u677f\u9009\u9879\u63d2\u56fe1\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>\u4e0b\u9762\u662f\u4ece Custom Category Templates \u63d2\u4ef6\u4e2d\u63d0\u53d6\u51fa\u6765\u7684\u4ee3\u7801\uff0c\u53ef\u4ee5\u76f4\u63a5\u6dfb\u52a0\u5230\u5f53\u524d\u4e3b\u9898\u51fd\u6570\u6a21\u677ffunctions.php\u4e2d\u5373\u53ef\u3002<\/p>\n<p>\u4ee3\u7801\u7248\uff1a<\/p>\n<pre>\/\/&nbsp;\u5206\u7c7b\u9009\u62e9\u6a21\u677f\nclass&nbsp;Select_Category_Template{\n\tpublic&nbsp;function&nbsp;__construct()&nbsp;{\n\t\tadd_filter(&nbsp;'category_template',&nbsp;array($this,'get_custom_category_template'&nbsp;));\n\t\tadd_action&nbsp;(&nbsp;'edit_category_form_fields',&nbsp;array($this,'category_template_meta_box'));\n\t\tadd_action(&nbsp;'category_add_form_fields',&nbsp;array(&nbsp;&amp;amp;$this,&nbsp;'category_template_meta_box')&nbsp;);\n\t\tadd_action(&nbsp;'created_category',&nbsp;array(&nbsp;&amp;amp;$this,&nbsp;'save_category_template'&nbsp;));\n\t\tadd_action&nbsp;(&nbsp;'edited_category',&nbsp;array($this,'save_category_template'));\n\t\tdo_action('Custom_Category_Template_constructor',$this);\n\t}\n&nbsp;\n\t\/\/&nbsp;\u6dfb\u52a0\u8868\u5355\u5230\u5206\u7c7b\u7f16\u8f91\u9875\u9762\n\tpublic&nbsp;function&nbsp;category_template_meta_box(&nbsp;$tag&nbsp;)&nbsp;{\n\t\t$t_id&nbsp;=&nbsp;$tag-&amp;gt;term_id;\n\t\t$cat_meta&nbsp;=&nbsp;get_option(&nbsp;\"category_templates\");\n\t\t$template&nbsp;=&nbsp;isset($cat_meta[$t_id])&nbsp;?&nbsp;$cat_meta[$t_id]&nbsp;:&nbsp;false;\n\t\t?&amp;gt;\n\t\t&lt;tr&gt;\n&lt;th&gt;&lt;label&gt;&lt;?php  _e(&#039;Category Template&#039;); ?&gt;&lt;\/label&gt;&lt;\/th&gt;\n\t\t\t&lt;td&gt;\n\t\t\t\t&lt;select&gt;&lt;option&gt;&lt;?php  _e(&#039;Default Template&#039;); ?&gt;&lt;\/option&gt;\n&lt;?php  page_template_dropdown($template); ?&gt;&lt;\/select&gt;&lt;br&gt;&lt;span&gt;&lt;?php  _e(&#039;\u4e3a\u6b64\u5206\u7c7b\u9009\u62e9\u4e00\u4e2a\u6a21\u677f&#039;); ?&gt;&lt;\/span&gt;\n\t\t\t&lt;\/td&gt;\n\t\t&lt;\/tr&gt;&lt;?php do_action(&#039;Custom_Category_Template_ADD_FIELDS&#039;,$tag);\n\t}\n \n\t\/\/ \u4fdd\u5b58\u8868\u5355\n\tpublic function save_category_template( $term_id ) {\n\t\tif ( isset( $_POST[&#039;cat_template&#039;] )) {\n\t\t\t$cat_meta = get_option( \"category_templates\");\n\t\t\t$cat_meta[$term_id] = $_POST[&#039;cat_template&#039;];\n\t\t\tupdate_option( \"category_templates\", $cat_meta );\n\t\t\tdo_action(&#039;Custom_Category_Template_SAVE_FIELDS&#039;,$term_id);\n\t\t}\n\t}\n \n\t\/\/ \u5904\u7406\u9009\u62e9\u7684\u5206\u7c7b\u6a21\u677f\n\tfunction get_custom_category_template( $category_template ) {\n\t\t$cat_ID = absint( get_query_var(&#039;cat&#039;) );\n\t\t$cat_meta = get_option(&#039;category_templates&#039;);\n\t\tif (isset($cat_meta[$cat_ID]) &amp;&amp; $cat_meta[$cat_ID] != &#039;default&#039; ){\n\t\t\t$temp = locate_template($cat_meta[$cat_ID]);\n\t\t\tif (!empty($temp))\n\t\t\t\treturn apply_filters(\"Custom_Category_Template_found\",$temp);\n\t\t}\n\t\treturn $category_template;\n\t}\n}\n \n$cat_template = new Select_Category_Template();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4e3aWordPress\u5206\u7c7b\u6dfb\u52a0\u9009\u62e9\u4e0d\u540c\u6a21\u677f\u9009\u9879\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>\u4e0b\u9762\u7531\/&#8221; target=&#8221;_blank&#8221;&gt;wordpress\u6559\u7a0b\u680f\u76ee\u7ed9\u5927\u5bb6\u4e3awordpress\u5206\u7c7b\u6dfb\u52a0\u9009\u62e9\u4e0d\u540c\u6a21\u677f\u9009\u9879\u7684\u65b9\u6cd5\uff0c\u5e0c\u671b\u5bf9\u9700\u8981\u7684\u670b\u53cb\u6709\u6240\u5e2e\u52a9\uff01 \u6211\u4eec\u6709\u65f6\u4f1a\u6839\u636e\u5206\u7c7b\u7684\u5185\u5bb9\uff0c\u60f3\u8ba9\u4e0d\u540c\u7684\u5206\u7c7b\u4ee5\u4e0d\u540c\u7684\u6837\u5f0f\u5c55\u793a\u3002\u901a\u5e38\u7684\u65b9\u6cd5\u662f\u5728\u5f53\u524d\u4e3b\u9898\u6839\u76ee\u5f55\u4e2d\u591a\u5efa\u51e0\u4e2a\u4e0d\u540c\u5e03\u5c40\u6837\u5f0f\u7684\u5206\u7c7b\u6a21\u677f\uff0c\u6bd4\u5982category-1.php\u3001category-2.php\u3001category-3.php&#8230;..\uff0c\u540e\u9762\u7684\u6570\u5b57\u662f\u5bf9\u5e94\u8be5\u7684\u5206\u7c7bID\u53f7\uff0c\u6216\u8005\u4f7f\u7528is_category()\u51fd\u6570\u6dfb\u52a0\u5224\u65ad\uff0c\u64cd\u4f5c\u6709\u4e9b\u7e41\u7410\u3002\u6709\u4e2a\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u5b89\u88c5 Custom Category Templates \u63d2\u4ef6\u3002 \u542f\u7528\u63d2\u4ef6\u540e\uff0c\u5728\u7f16\u8f91\u5206\u7c7b\u65f6\u4f1a\u6dfb\u52a0\u4e00\u4e2a\u9009\u62e9\u6a21\u677f\u7684\u9009\u9879\u3002 \u5236\u4f5c\u51e0\u4e2a\u4e0d\u540c\u5e03\u5c40\u98ce\u683c\u7684\u9875\u9762\u6a21\u677f\uff0c\u6a21\u677f\u5934\u90e8\u5fc5\u987b\u6709\u7c7b\u4f3c\u7684\u6807\u8bc6\uff1a &lt;?php \/* Template Name: \u6a21\u677fA *\/ \u767b\u5f55\u540e\u590d\u5236 \u7136\u540e\u5728\u7f16\u8f91\u6216\u8005\u6dfb\u52a0\u5206\u7c7b\u65f6\uff0c\u4e3a\u4e0d\u540c\u7684\u5206\u7c7b\u9009\u62e9\u4e13\u7528\u7684\u6a21\u677f\u5373\u53ef\u3002 \u6548\u679c\u5982\u56fe\uff1a &nbsp; \u4e0b\u9762\u662f\u4ece Custom Category Templates \u63d2\u4ef6\u4e2d\u63d0\u53d6\u51fa\u6765\u7684\u4ee3\u7801\uff0c\u53ef\u4ee5\u76f4\u63a5\u6dfb\u52a0\u5230\u5f53\u524d\u4e3b\u9898\u51fd\u6570\u6a21\u677ffunctions.php\u4e2d\u5373\u53ef\u3002 \u4ee3\u7801\u7248\uff1a \/\/&nbsp;\u5206\u7c7b\u9009\u62e9\u6a21\u677f class&nbsp;Select_Category_Template{ public&nbsp;function&nbsp;__construct()&nbsp;{ add_filter(&nbsp;&#8216;category_template&#8217;,&nbsp;array($this,&#8217;get_custom_category_template&#8217;&nbsp;)); add_action&nbsp;(&nbsp;&#8216;edit_category_form_fields&#8217;,&nbsp;array($this,&#8217;category_template_meta_box&#8217;)); add_action(&nbsp;&#8216;category_add_form_fields&#8217;,&nbsp;array(&nbsp;&amp;amp;$this,&nbsp;&#8216;category_template_meta_box&#8217;)&nbsp;); add_action(&nbsp;&#8216;created_category&#8217;,&nbsp;array(&nbsp;&amp;amp;$this,&nbsp;&#8216;save_category_template&#8217;&nbsp;)); add_action&nbsp;(&nbsp;&#8216;edited_category&#8217;,&nbsp;array($this,&#8217;save_category_template&#8217;)); do_action(&#8216;Custom_Category_Template_constructor&#8217;,$this); } &nbsp; \/\/&nbsp;\u6dfb\u52a0\u8868\u5355\u5230\u5206\u7c7b\u7f16\u8f91\u9875\u9762 public&nbsp;function&nbsp;category_template_meta_box(&nbsp;$tag&nbsp;)&nbsp;{ $t_id&nbsp;=&nbsp;$tag-&amp;gt;term_id; $cat_meta&nbsp;=&nbsp;get_option(&nbsp;&#8220;category_templates&#8221;); $template&nbsp;=&nbsp;isset($cat_meta[$t_id])&nbsp;?&nbsp;$cat_meta[$t_id]&nbsp;:&nbsp;false; ?&amp;gt; &lt;tr&gt; &lt;th&gt;&lt;label&gt;&lt;?php _e(&#039;Category Template&#039;); ?&gt;&lt;\/label&gt;&lt;\/th&gt; &lt;td&gt; &lt;select&gt;&lt;option&gt;&lt;?php _e(&#039;Default Template&#039;); ?&gt;&lt;\/option&gt; &lt;?php page_template_dropdown($template); ?&gt;&lt;\/select&gt;&lt;br&gt;&lt;span&gt;&lt;?php [&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-29485","post","type-post","status-publish","format-standard","hentry","category-cms"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/29485","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=29485"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/29485\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=29485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=29485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=29485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}