{"id":26,"date":"2025-07-30T09:44:12","date_gmt":"2025-07-30T01:44:12","guid":{"rendered":"http:\/\/www.wueryi.com\/?p=26"},"modified":"2025-07-30T09:44:12","modified_gmt":"2025-07-30T01:44:12","slug":"%e9%80%9a%e8%bf%87%e7%9f%ad%e4%bb%a3%e7%a0%81%e5%ae%9e%e7%8e%b0%e8%b0%83%e7%94%a8woocommerce%e6%9c%ac%e6%9c%88%e5%85%b3%e6%b3%a8%e5%ba%a6%e6%9c%80%e9%ab%98%e6%b5%8f%e8%a7%88%e9%87%8f%e6%9c%80","status":"publish","type":"post","link":"http:\/\/www.wueryi.com\/?p=26","title":{"rendered":"\u901a\u8fc7\u77ed\u4ee3\u7801\u5b9e\u73b0\u8c03\u7528woocommerce\u672c\u6708\u5173\u6ce8\u5ea6\u6700\u9ad8(\u6d4f\u89c8\u91cf\u6700\u9ad8)\u7684\u5546\u54c1"},"content":{"rendered":"\n<p>WooCommerce\u81ea\u5e26\u7684[products]\u77ed\u4ee3\u7801\u6ca1\u6709\u76f4\u63a5\u6309\u201c\u6d4f\u89c8\u91cf\u201d\u6392\u5e8f\u7684\u53c2\u6570\uff0c\u56e0\u6b64\u65e0\u6cd5\u4ec5\u901a\u8fc7\u4e00\u6bb5\u5b98\u65b9\u77ed\u4ee3\u7801\u5c31\u201c\u672c\u6708\u6d4f\u89c8\u91cf\u6700\u9ad8\u201d\u7684\u5546\u54c1\u3002<\/p>\n\n\n\n<p>\u8981\u5b9e\u73b0\u8fd9\u4e00\u9700\u6c42\uff0c\u5fc5\u987b\u6ee1\u8db3\u4e0b\u9762\u4e24\u4e2a\u524d\u63d0\uff1a<\/p>\n\n\n\n<p>\u7f51\u7ad9\u5b89\u88c5\u4e86\u7edf\u8ba1\u6d4f\u89c8\u91cf\u7684\u63d2\u4ef6(\u4f8b\u5982PostViewsCounter\u3001WPPostViews\u3001JetpackStats\u7b49)\uff0c\u5e76\u4e14\u628a\u6bcf\u5929\u7684\u6d4f\u89c8\u91cf\u5199\u5165\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5b57\u6bb5\uff0c\u5e38\u89c1\u7684\u5b57\u6bb5\u540d\u53ebpost_views\u6216_views_count\u3002<\/p>\n\n\n\n<p>\u7528\u51e0\u884cPHP\u628aWooCommerce\u77ed\u4ee3\u7801\u67e5\u8be2\u6539\u5199\u6210\u6309\u8fd9\u4e2a\u81ea\u5b9a\u4e49\u5b57\u6bb5\u3001\u4e14\u9650\u5b9a\u201c\u672c\u6708\u65f6\u95f4\u8303\u56f4\u201d\u6392\u5e8f\u3002<\/p>\n\n\n\n<p>\u628a\u4e0b\u9762\u4ee3\u7801\u4e00\u6b21\u6027\u653e\u8fdb\u4e3b\u9898(\u6216\u5b50\u4e3b\u9898)\u7684functions.php\uff0c\u5373\u53ef\u5728\u9875\u9762\/\u6587\u7ae0\u91cc\u76f4\u63a5\u7528\u77ed\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * \u77ed\u4ee3\u7801 &#91;most_viewed_this_month] \u5c55\u793a\u672c\u6708\u6d4f\u89c8\u91cf\u6700\u9ad8\u7684\u5546\u54c1\n * \u7528\u6cd5\u793a\u4f8b\uff1a&#91;most_viewed_this_month limit=\"6\" columns=\"3\"]\n *\/\nadd_shortcode( 'most_viewed_this_month', 'shortcode_most_viewed_this_month' );\nfunction shortcode_most_viewed_this_month( $atts ) {\n    $atts = shortcode_atts( &#91;\n        'limit'   => 4,\n        'columns' => 4,\n        'meta_key'=> 'post_views', \/\/ \u6309\u4f60\u5b9e\u9645\u63d2\u4ef6\u751f\u6210\u7684\u5b57\u6bb5\u540d\u4fee\u6539\n    ], $atts, 'most_viewed_this_month' );\n\n    \/\/ \u672c\u6708\u7b2c\u4e00\u5929 00:00:00\n    $month_start = date( 'Y-m-01 00:00:00' );\n\n    $query_args = &#91;\n        'post_type'      => 'product',\n        'post_status'    => 'publish',\n        'posts_per_page' => (int)$atts&#91;'limit'],\n        'meta_key'       => $atts&#91;'meta_key'],\n        'orderby'        => 'meta_value_num',\n        'order'          => 'DESC',\n        'date_query'     => &#91;\n            'after' => $month_start,\n        ],\n        'tax_query'      => &#91;  \/\/ \u6392\u9664\u975e\u516c\u5f00\u5546\u54c1\n            &#91;\n                'taxonomy' => 'product_visibility',\n                'field'    => 'name',\n                'terms'    => 'exclude-from-catalog',\n                'operator' => 'NOT IN',\n            ],\n        ],\n    ];\n\n    $products = new WP_Query( $query_args );\n    if ( ! $products->have_posts() ) {\n        return '&lt;p>\u6682\u65e0\u6570\u636e&lt;\/p>';\n    }\n\n    \/\/ \u4f7f\u7528 WooCommerce \u81ea\u5e26\u6a21\u677f\u8f93\u51fa\n    ob_start();\n    woocommerce_product_loop_start();\n    while ( $products->have_posts() ) {\n        $products->the_post();\n        wc_get_template_part( 'content', 'product' );\n    }\n    woocommerce_product_loop_end();\n    wp_reset_postdata();\n\n    \/\/ \u7528 WooCommerce \u81ea\u5e26 grid \u5305\u88c5\n    $loop_html = '&lt;div class=\"woocommerce columns-' . absint( $atts&#91;'columns'] ) . '\">' .\n                 ob_get_clean() . '&lt;\/div>';\n\n    return $loop_html;\n}<\/code><\/pre>\n\n\n\n<p>\u4f7f\u7528\u65b9\u6cd5\uff08\u77ed\u4ee3\u7801\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;most_viewed_this_month limit=\"6\" columns=\"3\"]<\/code><\/pre>\n\n\n\n<p>\u5373\u53ef\u5728\u4efb\u610f\u6587\u7ae0\u3001\u9875\u9762\u3001\u5c0f\u5de5\u5177\u6216Elementor\/HTML\u6a21\u5757\u91cc\u76f4\u63a5\u8c03\u7528\uff0c\u663e\u793a\u672c\u6708\u6d4f\u89c8\u91cf\u6700\u9ad8\u76846\u4e2a\u5546\u54c1\uff0c3\u5217\u6392\u7248\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u901a\u8fc7\u77ed\u4ee3\u7801\u5b9e\u73b0\u8c03\u7528woocommerce\u672c\u6708\u5173\u6ce8\u5ea6\u6700\u9ad8(\u6d4f\u89c8\u91cf\u6700\u9ad8)\u7684\u5546\u54c1.\u7528\u51e0\u884cPHP\u628aWooCommerce\u77ed\u4ee3\u7801\u67e5\u8be2\u6539\u5199\u6210\u6309\u8fd9\u4e2a\u81ea\u5b9a\u4e49\u5b57\u6bb5\u3001\u4e14\u9650\u5b9a\u201c\u672c\u6708\u65f6\u95f4\u8303\u56f4\u201d\u6392\u5e8f\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[24,25,23],"class_list":["post-26","post","type-post","status-publish","format-standard","hentry","category-woocommerce","tag-woocommerce"],"_links":{"self":[{"href":"http:\/\/www.wueryi.com\/index.php?rest_route=\/wp\/v2\/posts\/26","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.wueryi.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.wueryi.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.wueryi.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.wueryi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26"}],"version-history":[{"count":1,"href":"http:\/\/www.wueryi.com\/index.php?rest_route=\/wp\/v2\/posts\/26\/revisions"}],"predecessor-version":[{"id":27,"href":"http:\/\/www.wueryi.com\/index.php?rest_route=\/wp\/v2\/posts\/26\/revisions\/27"}],"wp:attachment":[{"href":"http:\/\/www.wueryi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.wueryi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.wueryi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}