{"id":4747,"date":"2025-10-20T18:10:04","date_gmt":"2025-10-20T10:10:04","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/wpcms\/bjjc\/4747.html"},"modified":"2025-11-07T10:21:12","modified_gmt":"2025-11-07T02:21:12","slug":"wordpress%e8%8e%b7%e5%8f%96%e8%87%aa%e5%ae%9a%e4%b9%89%e5%88%86%e7%b1%bb%e6%b3%95%e4%b8%ad%e6%89%80%e6%9c%89%e5%88%86%e7%b1%bb%e4%b8%8b%e7%9a%84%e6%89%80%e6%9c%89%e6%96%87%e7%ab%a0","status":"publish","type":"bjjc","link":"https:\/\/www.zhaozhao123.cn\/wpcms\/bjjc\/4747.html","title":{"rendered":"WordPress\u83b7\u53d6\u81ea\u5b9a\u4e49\u5206\u7c7b\u6cd5\u4e2d\u6240\u6709\u5206\u7c7b\u4e0b\u7684\u6240\u6709\u6587\u7ae0"},"content":{"rendered":"<p>\u60f3\u5728WordPress\u4e2d\u83b7\u53d6\u81ea\u5b9a\u4e49\u5206\u7c7b\u6cd5 <code>tuisong<\/code> \u4e0b\u6240\u6709\u5206\u7c7b\u7684\u6240\u6709\u6587\u7ae0\uff0c\u6838\u5fc3\u65b9\u6cd5\u662f\u4f7f\u7528 <code>WP_Query<\/code> \u5e76\u7ed3\u5408 <code>tax_query<\/code> \u53c2\u6570\u3002\u4e0b\u9762\u6211\u5c06\u4e3a\u4f60\u8be6\u7ec6\u89e3\u91ca\u5982\u4f55\u64cd\u4f5c\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5b9e\u7528\u793a\u4f8b\u3002<\/p><h2 class=\"wp-block-heading\">\u6838\u5fc3\u65b9\u6cd5\uff1a\u4f7f\u7528 WP_Query<\/h2><p>\u4f60\u53ef\u4ee5\u901a\u8fc7 <code>WP_Query<\/code> \u6765\u67e5\u8be2\u7279\u5b9a\u81ea\u5b9a\u4e49\u5206\u7c7b\u6cd5\u4e0b\u7684\u6240\u6709\u6587\u7ae0\uff0c\u65e0\u8bba\u5c5e\u4e8e\u54ea\u4e2a\u5177\u4f53\u5206\u7c7b\u3002\u4e0b\u9762\u662f\u57fa\u672c\u7684\u4ee3\u7801\u7ed3\u6784\uff1a<\/p><pre class=\"wp-block-code\"><code>&lt;?php\n$args = array(\n    'post_type'      =&gt; 'post', \/\/ \u5982\u679c\u662f\u81ea\u5b9a\u4e49\u6587\u7ae0\u7c7b\u578b\uff0c\u66ff\u6362\u4e3a\u4f60\u7684\u6587\u7ae0\u7c7b\u578b\u540d\u79f0\uff0c\u4f8b\u5982 'my_custom_post'\n    'posts_per_page' =&gt; -1,     \/\/ \u83b7\u53d6\u6240\u6709\u6587\u7ae0\uff0c\u8bbe\u7f6e-1\u8868\u793a\u83b7\u53d6\u6240\u6709\u6587\u7ae0\uff0c\u4f46\u9700\u6ce8\u610f\u6027\u80fd\n    'tax_query'      =&gt; array(\n        array(\n            'taxonomy' =&gt; 'tuisong', \/\/ \u4f60\u7684\u81ea\u5b9a\u4e49\u5206\u7c7b\u6cd5\u540d\u79f0\n            'operator' =&gt; 'EXISTS'   \/\/ \u83b7\u53d6\u6240\u6709\u5177\u6709\u6b64\u5206\u7c7b\u6cd5\u7684\u6587\u7ae0\uff0c\u65e0\u8bba\u5177\u4f53\u672f\u8bed\n        )\n    )\n);\n\n$query = new WP_Query($args);\n\nif ($query-&gt;have_posts()) {\n    while ($query-&gt;have_posts()) {\n        $query-&gt;the_post();\n        \/\/ \u5728\u8fd9\u91cc\u8f93\u51fa\u6587\u7ae0\u4fe1\u606f\uff0c\u4f8b\u5982\uff1a\n        echo '&lt;h2&gt;' . get_the_title() . '&lt;\/h2&gt;';\n        echo '&lt;div&gt;' . get_the_excerpt() . '&lt;\/div&gt;';\n        echo '&lt;a href=\"' . get_permalink() . '\"&gt;\u9605\u8bfb\u66f4\u591a&lt;\/a&gt;';\n    }\n} else {\n    echo '\u6ca1\u6709\u627e\u5230\u76f8\u5173\u6587\u7ae0\u3002';\n}\n\nwp_reset_postdata(); \/\/ \u91cd\u7f6e\u6587\u7ae0\u6570\u636e\n?&gt;<\/code><\/pre><h2 class=\"wp-block-heading\">\u83b7\u53d6\u7279\u5b9a\u5206\u7c7b\u4e0b\u7684\u6587\u7ae0<\/h2><p>\u5982\u679c\u4f60\u53ea\u60f3\u83b7\u53d6 <code>tuisong<\/code> \u5206\u7c7b\u6cd5\u4e2d<strong>\u67d0\u4e2a\u7279\u5b9a\u5206\u7c7b<\/strong>\u4e0b\u7684\u6240\u6709\u6587\u7ae0\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u3002\u8fd9\u91cc\u5047\u8bbe\u4f60\u77e5\u9053\u8be5\u5206\u7c7b\u7684\u672f\u8bedID\u3001\u522b\u540d\u6216\u540d\u79f0\u3002<\/p><h3 class=\"wp-block-heading\">1. \u6309\u5206\u7c7b\u672f\u8bedID\u67e5\u8be2<\/h3><pre class=\"wp-block-code\"><code>$args = array(\n    'post_type'      =&gt; 'post', \/\/ \u6839\u636e\u9700\u8981\u66ff\u6362\u4e3a\u4f60\u7684\u6587\u7ae0\u7c7b\u578b\n    'posts_per_page' =&gt; -1,\n    'tax_query'      =&gt; array(\n        array(\n            'taxonomy' =&gt; 'tuisong',\n            'field'    =&gt; 'term_id', \/\/ \u6309\u672f\u8bedID\u67e5\u8be2\n            'terms'    =&gt; array(123, 456), \/\/ \u66ff\u6362\u4e3a\u4f60\u7684\u5177\u4f53\u5206\u7c7b\u672f\u8bedID\u6570\u7ec4\n            'operator' =&gt; 'IN' \/\/ \u5305\u542b\u6240\u6709\u6307\u5b9a\u672f\u8bed\u7684\u6587\u7ae0\n        )\n    )\n);<\/code><\/pre><h3 class=\"wp-block-heading\">2. \u6309\u5206\u7c7b\u672f\u8bed\u522b\u540d\u67e5\u8be2<\/h3><pre class=\"wp-block-code\"><code>$args = array(\n    'post_type'      =&gt; 'post',\n    'posts_per_page' =&gt; -1,\n    'tax_query'      =&gt; array(\n        array(\n            'taxonomy' =&gt; 'tuisong',\n            'field'    =&gt; 'slug', \/\/ \u6309\u672f\u8bed\u522b\u540d\u67e5\u8be2\n            'terms'    =&gt; array('some-term-slug'), \/\/ \u66ff\u6362\u4e3a\u4f60\u7684\u5177\u4f53\u5206\u7c7b\u672f\u8bed\u522b\u540d\u6570\u7ec4\n            'operator' =&gt; 'IN'\n        )\n    )\n);<\/code><\/pre><h2 class=\"wp-block-heading\">\u83b7\u53d6\u5206\u7c7b\u6cd5\u4e2d\u7684\u5206\u7c7b\u672f\u8bed<\/h2><p>\u6709\u65f6\u5019\uff0c\u4f60\u53ef\u80fd\u60f3\u5148\u83b7\u53d6\u81ea\u5b9a\u4e49\u5206\u7c7b\u6cd5 <code>tuisong<\/code> \u4e0b\u7684\u6240\u6709\u5206\u7c7b\u672f\u8bed\uff0c\u7136\u540e\u518d\u5faa\u73af\u83b7\u53d6\u6bcf\u4e2a\u672f\u8bed\u4e0b\u7684\u6587\u7ae0\u3002<\/p><pre class=\"wp-block-code\"><code>\/\/ \u83b7\u53d6tuisong\u5206\u7c7b\u6cd5\u4e2d\u7684\u6240\u6709\u5206\u7c7b\u672f\u8bed\n$terms = get_terms(array(\n    'taxonomy'   =&gt; 'tuisong',\n    'hide_empty' =&gt; false, \/\/ \u662f\u5426\u9690\u85cf\u7a7a\u5206\u7c7b\n));\n\nif (!empty($terms) &amp;&amp; !is_wp_error($terms)) {\n    foreach ($terms as $term) {\n        \/\/ \u83b7\u53d6\u5f53\u524d\u672f\u8bed\u4e0b\u7684\u6587\u7ae0\n        $post_args = array(\n            'post_type'      =&gt; 'post',\n            'posts_per_page' =&gt; -1,\n            'tax_query'      =&gt; array(\n                array(\n                    'taxonomy' =&gt; 'tuisong',\n                    'field'    =&gt; 'slug',\n                    'terms'    =&gt; $term-&gt;slug,\n                )\n            )\n        );\n        $term_query = new WP_Query($post_args);\n        \/\/ ... \u7136\u540e\u5faa\u73af\u8f93\u51fa\u6587\u7ae0\uff0c\u540c\u4e0a\n        wp_reset_postdata();\n    }\n}<\/code><\/pre><h2 class=\"wp-block-heading\">\u5173\u952e\u53c2\u6570\u8bf4\u660e<\/h2><p>\u4e0b\u8868\u603b\u7ed3\u4e86 <code>WP_Query<\/code> \u4e2d <code>tax_query<\/code> \u90e8\u5206\u7684\u4e00\u4e9b\u5e38\u7528\u53c2\u6570\u9009\u9879\uff0c\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5b9a\u5236\u4f60\u7684\u67e5\u8be2\uff1a<\/p><figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th><strong>\u53c2\u6570<\/strong><\/th><th><strong>\u63cf\u8ff0<\/strong><\/th><th><strong>\u5e38\u7528\u503c<\/strong><\/th><\/tr><\/thead><tbody><tr><td><code>taxonomy<\/code><\/td><td>\u8981\u67e5\u8be2\u7684\u81ea\u5b9a\u4e49\u5206\u7c7b\u6cd5\u540d\u79f0<\/td><td>&#8216;tuisong&#8217;\uff08\u4f60\u7684\u5206\u7c7b\u6cd5\u540d\u79f0\uff09<\/td><\/tr><tr><td><code>field<\/code><\/td><td>\u7528\u4e8e\u5339\u914d\u672f\u8bed\u7684\u5b57\u6bb5<\/td><td>&#8216;term_id&#8217;, &#8216;slug&#8217;, &#8216;name&#8217;<\/td><\/tr><tr><td><code>terms<\/code><\/td><td>\u8981\u5339\u914d\u7684\u672f\u8bed\u503c\uff08\u6570\u7ec4\u6216\u5b57\u7b26\u4e32\uff09<\/td><td>array(123, 456), &#8216;some-term&#8217;<\/td><\/tr><tr><td><code>operator<\/code><\/td><td>\u672f\u8bed\u67e5\u8be2\u7684\u903b\u8f91\u64cd\u4f5c\u7b26<\/td><td>&#8216;IN&#8217;\uff08\u5305\u542b\uff09, &#8216;NOT IN&#8217;\uff08\u6392\u9664\uff09, &#8216;AND&#8217;\uff08\u5168\u90e8\u5305\u542b\uff09<\/td><\/tr><tr><td><code>include_children<\/code><\/td><td>\u662f\u5426\u5305\u542b\u5b50\u672f\u8bed\uff08\u901a\u5e38\u5bf9\u4e8e\u5206\u5c42\u5206\u7c7b\u6cd5\u5982\u7c7b\u522b\u6709\u6548\uff09<\/td><td>true\uff08\u9ed8\u8ba4\uff09, false<\/td><\/tr><tr><td><code>posts_per_page<\/code><\/td><td>\u8981\u83b7\u53d6\u7684\u6587\u7ae0\u6570\u91cf\uff0c-1\u8868\u793a\u83b7\u53d6\u6240\u6709\u6587\u7ae0<\/td><td>10, -1<\/td><\/tr><\/tbody><\/table><\/figure><h2 class=\"wp-block-heading\">\u6027\u80fd\u4e0e\u5206\u9875\u5efa\u8bae<\/h2><p><strong>\u8c28\u614e\u4f7f\u7528 <code>'posts_per_page' =&gt; -1<\/code><\/strong>\uff1a\u8fd9\u53ef\u80fd\u4f1a\u62d6\u6162\u5927\u578b\u7f51\u7ad9\u7684\u52a0\u8f7d\u901f\u5ea6\u3002\u5728\u751f\u4ea7\u73af\u5883\u4e2d\uff0c<strong>\u5f3a\u70c8\u5efa\u8bae\u8bbe\u7f6e\u5408\u7406\u7684\u5206\u9875<\/strong>\u3002<\/p><p><strong>\u5b9e\u73b0\u5206\u9875<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528 <code>'paged'<\/code> \u53c2\u6570\u3002<\/p><pre class=\"wp-block-code\"><code>$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;\n$args = array(\n    \/\/ ... \u5176\u4ed6\u53c2\u6570\n    'posts_per_page' =&gt; 10, \/\/ \u6bcf\u9875\u663e\u793a10\u7bc7\u6587\u7ae0\n    'paged'          =&gt; $paged,\n);<\/code><\/pre><p>\u4e4b\u540e\u4f60\u9700\u8981\u5728\u5faa\u73af\u7ed3\u675f\u540e\u6dfb\u52a0\u5206\u9875\u5bfc\u822a\u4ee3\u7801\u3002<\/p><p><strong>\u8003\u8651\u7f13\u5b58<\/strong>\uff1a\u5bf9\u4e8e\u4e0d\u5e38\u66f4\u65b0\u7684\u67e5\u8be2\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7f13\u5b58\u63d2\u4ef6\u6765\u4f18\u5316\u6027\u80fd\u3002<\/p><h2 class=\"wp-block-heading\">\u5176\u4ed6\u76f8\u5173\u51fd\u6570<\/h2><ul class=\"wp-block-list\">\n<li><code>get_posts()<\/code>\uff1a<code>WP_Query<\/code> \u7684\u4e00\u4e2a\u7b80\u5355\u66ff\u4ee3\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u67e5\u8be2\uff0c\u8fd4\u56de\u6587\u7ae0\u6570\u7ec4\u800c\u975e\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li><code>get_terms()<\/code>\uff1a\u83b7\u53d6\u5206\u7c7b\u6cd5\u4e2d\u7684\u672f\u8bed\u5217\u8868\uff0c\u5982\u4e0a\u6587\u793a\u4f8b\u6240\u793a\u3002<\/li>\n\n\n\n<li><code>wp_get_post_terms()<\/code>\uff1a\u83b7\u53d6\u67d0\u7bc7\u6587\u7ae0\u6240\u5c5e\u7684\u7279\u5b9a\u5206\u7c7b\u6cd5\u7684\u6240\u6709\u672f\u8bed\u3002<\/li>\n<\/ul><hr class=\"wp-block-separator has-alpha-channel-opacity\"><p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong>\uff1a<\/p><ul class=\"wp-block-list\">\n<li>\u5c06\u4ee3\u7801\u4e2d\u7684 <code>'tuisong'<\/code> \u66ff\u6362\u4e3a\u4f60\u7684\u5b9e\u9645\u81ea\u5b9a\u4e49\u5206\u7c7b\u6cd5\u540d\u79f0\u3002<\/li>\n\n\n\n<li>\u5c06 <code>'post'<\/code> (<code>'post_type'<\/code> \u53c2\u6570) \u66ff\u6362\u4e3a\u4f60\u7684\u81ea\u5b9a\u4e49\u6587\u7ae0\u7c7b\u578b\u540d\u79f0\uff08\u5982\u679c\u4e0d\u662f\u9ed8\u8ba4\u7684&#8221;\u6587\u7ae0&#8221;\uff09\u3002<\/li>\n\n\n\n<li>\u5728\u4f7f\u7528\u5206\u9875\u65f6\uff0c\u786e\u4fdd\u4f60\u7684WordPress\u56fa\u5b9a\u94fe\u63a5\u8bbe\u7f6e\u6b63\u786e\u3002<\/li>\n\n\n\n<li>\u5904\u7406\u5206\u7c7b\u672f\u8bed\u65f6\uff0c\u603b\u662f\u68c0\u67e5 <code>!is_wp_error($terms)<\/code> \u662f\u4e2a\u597d\u4e60\u60ef\u3002<\/li>\n<\/ul><p>\u5e0c\u671b\u4ee5\u4e0a\u4fe1\u606f\u80fd\u5e2e\u52a9\u4f60\u987a\u5229\u83b7\u53d6\u5230\u6240\u9700\u7684\u81ea\u5b9a\u4e49\u5206\u7c7b\u6cd5\u6587\u7ae0\u3002<\/p>","protected":false},"excerpt":{"rendered":"<p>\u60f3\u5728WordPress\u4e2d\u83b7\u53d6\u81ea\u5b9a\u4e49\u5206\u7c7b\u6cd5 tuisong \u4e0b\u6240\u6709\u5206\u7c7b\u7684\u6240\u6709\u6587\u7ae0\uff0c\u6838\u5fc3\u65b9\u6cd5\u662f\u4f7f\u7528 WP_Query \u5e76\u7ed3\u5408 tax_query \u53c2\u6570\u3002\u4e0b\u9762\u6211\u5c06\u4e3a\u4f60\u8be6\u7ec6\u89e3\u91ca\u5982\u4f55\u64cd\u4f5c\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5b9e\u7528\u793a\u4f8b\u3002 \u6838\u5fc3\u65b9\u6cd5\uff1a\u4f7f\u7528 WP_Query \u4f60\u53ef\u4ee5\u901a\u8fc7 WP_Query \u6765\u67e5\u8be2\u7279\u5b9a\u81ea\u5b9a\u4e49\u5206\u7c7b\u6cd5\u4e0b\u7684\u6240\u6709\u6587\u7ae0\uff0c\u65e0\u8bba\u5c5e\u4e8e\u54ea\u4e2a\u5177\u4f53\u5206\u7c7b..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":true},"tags":[3267],"bjjc2nav":[14],"tuisongtax":[],"class_list":["post-4747","bjjc","type-bjjc","status-publish","hentry","tag-hqwzdffdq","bjjc2nav-jdztkf"],"acf":{"qian_art_seotitle":"","qian_art_seotitle_source":{"label":"SEO\u6807\u9898","type":"text","formatted_value":""},"qian_art_seokws":"","qian_art_seokws_source":{"label":"SEO\u5173\u952e\u8bcd","type":"text","formatted_value":""},"qian_art_stzhong":"\u5206\u7c7b\u6cd5\u4e0b\u6240\u6709\u6587\u7ae0","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":"\u5206\u7c7b\u6cd5\u4e0b\u6240\u6709\u6587\u7ae0"}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/bjjc\/4747","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/bjjc"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/types\/bjjc"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/users\/1"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/media?parent=4747"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/tags?post=4747"},{"taxonomy":"bjjc2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/bjjc2nav?post=4747"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/wpcms\/wp-json\/wp\/v2\/tuisongtax?post=4747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}