{"id":32714,"date":"2024-11-25T11:59:07","date_gmt":"2024-11-25T03:59:07","guid":{"rendered":"https:\/\/fwq.ai\/blog\/32714\/"},"modified":"2024-11-25T11:59:07","modified_gmt":"2024-11-25T03:59:07","slug":"%e5%a6%82%e4%bd%95%e4%b8%bawordpress%e6%8f%92%e4%bb%b6%e6%b7%bb%e5%8a%a0%e6%96%87%e7%ab%a0%e5%88%86%e7%b1%bb%e7%ae%a1%e7%90%86%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/32714\/","title":{"rendered":"\u5982\u4f55\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u6587\u7ae0\u5206\u7c7b\u7ba1\u7406\u529f\u80fd"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169387462466073.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u6587\u7ae0\u5206\u7c7b\u7ba1\u7406\u529f\u80fd\u63d2\u56fe\" alt=\"\u5982\u4f55\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u6587\u7ae0\u5206\u7c7b\u7ba1\u7406\u529f\u80fd\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u6587\u7ae0\u5206\u7c7b\u7ba1\u7406\u529f\u80fd<\/p>\n<p>WordPress\u662f\u76ee\u524d\u4f7f\u7528\u6700\u5e7f\u6cdb\u7684\u5185\u5bb9\u7ba1\u7406\u7cfb\u7edf\u4e4b\u4e00\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u63d2\u4ef6\u6765\u6269\u5c55\u5176\u529f\u80fd\u3002\u5982\u679c\u4f60\u662f\u4e00\u4e2a\u63d2\u4ef6\u5f00\u53d1\u8005\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u9700\u8981\u4e3a\u4f60\u7684\u63d2\u4ef6\u6dfb\u52a0\u6587\u7ae0\u5206\u7c7b\u7ba1\u7406\u529f\u80fd\u7684\u9700\u6c42\u3002\u672c\u6587\u5c06\u4e3a\u4f60\u4ecb\u7ecd\u5982\u4f55\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u6587\u7ae0\u5206\u7c7b\u7ba1\u7406\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u4f9b\u53c2\u8003\u3002<\/p>\n<ol>\n<li>\u521b\u5efa\u5206\u7c7b<br \/>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4e3a\u63d2\u4ef6\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u7ae0\u5206\u7c7b\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528register_taxonomy()\u51fd\u6570\u6765\u5b8c\u6210\u8fd9\u4e00\u4efb\u52a1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/li>\n<\/ol>\n<pre>\/\/ \u5728\u63d2\u4ef6\u7684\u4e3b\u6d3b\u52a8\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\nfunction custom_plugin_taxonomy() {\n    $labels = array(\n        'name' =&amp;gt; _x( '\u63d2\u4ef6\u5206\u7c7b', 'taxonomy general name', 'textdomain' ),\n        'singular_name' =&amp;gt; _x( '\u63d2\u4ef6\u5206\u7c7b', 'taxonomy singular name', 'textdomain' ),\n        'search_items' =&amp;gt;  __( '\u641c\u7d22\u5206\u7c7b', 'textdomain' ),\n        'all_items' =&amp;gt; __( '\u6240\u6709\u5206\u7c7b', 'textdomain' ),\n        'parent_item' =&amp;gt; __( '\u7236\u7ea7\u5206\u7c7b', 'textdomain' ),\n        'parent_item_colon' =&amp;gt; __( '\u7236\u7ea7\u5206\u7c7b\uff1a', 'textdomain' ),\n        'edit_item' =&amp;gt; __( '\u7f16\u8f91\u5206\u7c7b', 'textdomain' ),\n        'update_item' =&amp;gt; __( '\u66f4\u65b0\u5206\u7c7b', 'textdomain' ),\n        'add_new_item' =&amp;gt; __( '\u6dfb\u52a0\u65b0\u5206\u7c7b', 'textdomain' ),\n        'new_item_name' =&amp;gt; __( '\u65b0\u5206\u7c7b\u540d\u79f0', 'textdomain' ),\n        'menu_name' =&amp;gt; __( '\u5206\u7c7b', 'textdomain' ),\n    );\n\n    $args = array(\n        'hierarchical' =&amp;gt; true,\n        'labels' =&amp;gt; $labels,\n        'show_ui' =&amp;gt; true,\n        'show_admin_column' =&amp;gt; true,\n        'query_var' =&amp;gt; true,\n        'rewrite' =&amp;gt; array( 'slug' =&amp;gt; 'plugin_category' ),\n    );\n\n    register_taxonomy( 'plugin_category', array( 'post' ), $args );\n}\nadd_action( 'init', 'custom_plugin_taxonomy', 0 );<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528register_taxonomy()\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aplugin_category\u7684\u65b0\u6587\u7ae0\u5206\u7c7b\u3002\u8fd9\u4e2a\u5206\u7c7b\u5177\u6709\u4e00\u4e9b\u57fa\u672c\u5c5e\u6027\uff0c\u4f8b\u5982\u540d\u79f0\u3001\u641c\u7d22\u6587\u672c\u548c\u7f16\u8f91\u64cd\u4f5c\u7b49\u3002<\/p>\n<ol>\n<li>\u4e3a\u63d2\u4ef6\u542f\u7528\u5206\u7c7b\u7ba1\u7406<br \/>\u73b0\u5728\uff0c\u6211\u4eec\u9700\u8981\u5728\u63d2\u4ef6\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u754c\u9762\uff0c\u8ba9\u7528\u6237\u53ef\u4ee5\u5728\u6587\u7ae0\u7f16\u8f91\u9875\u9762\u4e2d\u9009\u62e9\u548c\u7ba1\u7406\u5206\u7c7b\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u94a9\u5b50\u51fd\u6570add_meta_box()\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/li>\n<\/ol>\n<pre>\/\/ \u5728\u63d2\u4ef6\u7684\u4e3b\u6d3b\u52a8\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\nfunction custom_plugin_taxonomy_meta_box() {\n    add_meta_box(\n        'plugin_category',\n        __( '\u63d2\u4ef6\u5206\u7c7b', 'textdomain' ),\n        'custom_plugin_taxonomy_meta_box_callback',\n        'post',\n        'side',\n        'default'\n    );\n}\nadd_action( 'add_meta_boxes', 'custom_plugin_taxonomy_meta_box' );\n\nfunction custom_plugin_taxonomy_meta_box_callback( $post ) {\n    wp_nonce_field( 'custom_plugin_taxonomy_meta_box', 'custom_plugin_taxonomy_meta_box_nonce' );\n\n    $terms = get_terms( array(\n        'taxonomy' =&amp;gt; 'plugin_category',\n        'hide_empty' =&amp;gt; false,\n    ) );\n\n    if ( ! empty( $terms ) &amp;amp;&amp;amp; ! is_wp_error( $terms ) ) {\n        echo '&lt;select name=\"plugin_category\"&gt;';\n        foreach ( $terms as $term ) {\n            echo '&lt;option value=\"' . $term-&amp;gt;term_id . '\"&gt;' . $term-&amp;gt;name . '&lt;\/option&gt;';\n        }\n        echo '&lt;\/select&gt;';\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528add_meta_box()\u51fd\u6570\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u5143\u6846\uff0c\u7528\u4e8e\u663e\u793a\u5206\u7c7b\u9009\u62e9\u6846\u3002\u5728custom_plugin_taxonomy_meta_box_callback()\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528get_terms()\u51fd\u6570\u83b7\u53d6\u6240\u6709\u53ef\u7528\u7684\u5206\u7c7b\uff0c\u5e76\u8f93\u51fa\u4e00\u4e2a\u4e0b\u62c9\u83dc\u5355\u4f9b\u7528\u6237\u9009\u62e9\u3002<\/p>\n<ol>\n<li>\u4fdd\u5b58\u548c\u66f4\u65b0\u5206\u7c7b<br \/>\u6700\u540e\uff0c\u6211\u4eec\u9700\u8981\u6dfb\u52a0\u4ee3\u7801\u6765\u4fdd\u5b58\u548c\u66f4\u65b0\u6240\u9009\u7684\u5206\u7c7b\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u94a9\u5b50\u51fd\u6570save_post\u6765\u5904\u7406\u8fd9\u4e2a\u4efb\u52a1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/li>\n<\/ol>\n<pre>\/\/ \u5728\u63d2\u4ef6\u7684\u4e3b\u6d3b\u52a8\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\nfunction custom_plugin_taxonomy_save_meta_box_data( $post_id ) {\n    if ( ! isset( $_POST['plugin_category'] ) ) {\n        return;\n    }\n\n    if ( ! wp_verify_nonce( $_POST['custom_plugin_taxonomy_meta_box_nonce'], 'custom_plugin_taxonomy_meta_box' ) ) {\n        return;\n    }\n\n    if ( defined( 'DOING_AUTOSAVE' ) &amp;amp;&amp;amp; DOING_AUTOSAVE ) {\n        return;\n    }\n\n    if ( ! current_user_can( 'edit_post', $post_id ) ) {\n        return;\n    }\n\n    $term_id = intval( $_POST['plugin_category'] );\n    wp_set_post_terms( $post_id, array( $term_id ), 'plugin_category' );\n}\nadd_action( 'save_post', 'custom_plugin_taxonomy_save_meta_box_data' );<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u68c0\u67e5\u4e86\u5206\u7c7b\u9009\u62e9\u6846\u662f\u5426\u88ab\u9009\u4e2d\uff0c\u5e76\u4f7f\u7528wp_verify_nonce()\u51fd\u6570\u9a8c\u8bc1\u63d0\u4ea4\u7684\u8868\u5355\u6570\u636e\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528wp_set_post_terms()\u51fd\u6570\u5c06\u6240\u9009\u5206\u7c7b\u4fdd\u5b58\u5230\u5f53\u524d\u6587\u7ae0\u4e2d\u3002<\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u5df2\u7ecf\u6210\u529f\u4e3a\u4f60\u7684WordPress\u63d2\u4ef6\u6dfb\u52a0\u4e86\u6587\u7ae0\u5206\u7c7b\u7ba1\u7406\u529f\u80fd\u3002\u7528\u6237\u73b0\u5728\u53ef\u4ee5\u5728\u6587\u7ae0\u7f16\u8f91\u9875\u9762\u4e2d\u9009\u62e9\u548c\u7ba1\u7406\u5206\u7c7b\uff0c\u4ee5\u5b9e\u73b0\u66f4\u597d\u7684\u5185\u5bb9\u7ba1\u7406\u4f53\u9a8c\u3002<\/p>\n<p>\u603b\u7ed3<br \/>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u6587\u7ae0\u5206\u7c7b\u7ba1\u7406\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u4e86\u76f8\u5173\u4ee3\u7801\u793a\u4f8b\u3002\u901a\u8fc7\u4f7f\u7528register_taxonomy()\u51fd\u6570\u521b\u5efa\u5206\u7c7b\uff0c\u4f7f\u7528add_meta_box()\u51fd\u6570\u6dfb\u52a0\u754c\u9762\uff0c\u4ee5\u53ca\u4f7f\u7528save_post\u94a9\u5b50\u51fd\u6570\u4fdd\u5b58\u548c\u66f4\u65b0\u5206\u7c7b\uff0c\u4f60\u53ef\u4ee5\u5c06\u8fd9\u4e00\u529f\u80fd\u5feb\u901f\u5730\u96c6\u6210\u5230\u81ea\u5df1\u7684\u63d2\u4ef6\u4e2d\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u4f60\u7f16\u5199\u51fa\u66f4\u52a0\u5f3a\u5927\u7684WordPress\u63d2\u4ef6\uff01<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u6587\u7ae0\u5206\u7c7b\u7ba1\u7406\u529f\u80fd\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\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u6587\u7ae0\u5206\u7c7b\u7ba1\u7406\u529f\u80fd WordPress\u662f\u76ee\u524d\u4f7f\u7528\u6700\u5e7f\u6cdb\u7684\u5185\u5bb9\u7ba1\u7406\u7cfb\u7edf\u4e4b\u4e00\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u63d2\u4ef6\u6765\u6269\u5c55\u5176\u529f\u80fd\u3002\u5982\u679c\u4f60\u662f\u4e00\u4e2a\u63d2\u4ef6\u5f00\u53d1\u8005\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u9700\u8981\u4e3a\u4f60\u7684\u63d2\u4ef6\u6dfb\u52a0\u6587\u7ae0\u5206\u7c7b\u7ba1\u7406\u529f\u80fd\u7684\u9700\u6c42\u3002\u672c\u6587\u5c06\u4e3a\u4f60\u4ecb\u7ecd\u5982\u4f55\u4e3aWordPress\u63d2\u4ef6\u6dfb\u52a0\u6587\u7ae0\u5206\u7c7b\u7ba1\u7406\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u4f9b\u53c2\u8003\u3002 \u521b\u5efa\u5206\u7c7b\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4e3a\u63d2\u4ef6\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u7ae0\u5206\u7c7b\u3002\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528register_taxonomy()\u51fd\u6570\u6765\u5b8c\u6210\u8fd9\u4e00\u4efb\u52a1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a \/\/ \u5728\u63d2\u4ef6\u7684\u4e3b\u6d3b\u52a8\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801 function custom_plugin_taxonomy() { $labels = array( &#8216;name&#8217; =&amp;gt; _x( &#8216;\u63d2\u4ef6\u5206\u7c7b&#8217;, &#8216;taxonomy general name&#8217;, &#8216;textdomain&#8217; ), &#8216;singular_name&#8217; =&amp;gt; _x( &#8216;\u63d2\u4ef6\u5206\u7c7b&#8217;, &#8216;taxonomy singular name&#8217;, &#8216;textdomain&#8217; ), &#8216;search_items&#8217; =&amp;gt; __( &#8216;\u641c\u7d22\u5206\u7c7b&#8217;, &#8216;textdomain&#8217; ), &#8216;all_items&#8217; =&amp;gt; __( &#8216;\u6240\u6709\u5206\u7c7b&#8217;, &#8216;textdomain&#8217; ), &#8216;parent_item&#8217; =&amp;gt; __( &#8216;\u7236\u7ea7\u5206\u7c7b&#8217;, &#8216;textdomain&#8217; ), &#8216;parent_item_colon&#8217; =&amp;gt; __( &#8216;\u7236\u7ea7\u5206\u7c7b\uff1a&#8217;, &#8216;textdomain&#8217; ), &#8216;edit_item&#8217; =&amp;gt; [&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-32714","post","type-post","status-publish","format-standard","hentry","category-cms"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32714","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=32714"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32714\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=32714"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=32714"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=32714"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}