【问题标题】:Limit displayed products on Woocommerce product category archives限制在 Woocommerce 产品类别档案中显示的产品
【发布时间】:2019-06-22 05:39:44
【问题描述】:

我有一家商店,每个类别的产品超过 30,000 件,我想将每个类别的产品显示限制为 1000 个,以加快加载速度。其余产品只能通过搜索找到。

我该怎么做?

【问题讨论】:

    标签: php wordpress woocommerce hook product


    【解决方案1】:

    你必须把这段代码放到functions.php文件中

    function product_pagination_by_category() {
        if( is_product_category() )
            $limit = 10;
    
        return $limit;
    }
    add_filter( 'loop_shop_per_page', 'product_pagination_by_category');
    

    【讨论】:

    • 感谢您的代码。但它是每页显示 100 个产品的代码。我想要的是每页只显示 10 个产品,每个类别总共 1000 个产品 = 要显示的 100 个产品页面。
    • 因此您可以使用 $limit = 10 来表示每页 10 个产品。
    • 那么如何限制每个类别的产品页面显示?只显示 100 页?
    • @PaOp 你的意思是延迟加载?
    • 您能否将您的代码添加到问题中?所以我可以分析它并可以帮助你。@PaOp
    【解决方案2】:

    如果您不想编写自定义代码,请使用名为 Load More Products for WooCommerce 的用户插件。

    通过 AJAX 无限滚动或加载更多产品按钮从下一页加载产品

    特点:

    ✅ WooCommerce 产品的无限滚动

    ✅ 加载更多产品按钮或 AJAX 分页

    ✅自定义按钮文字

    ✅ 自定义代码的 JavaScript 钩子

    查看网址:https://wordpress.org/plugins/load-more-products-for-woocommerce/

    如果您想获取代码,则可以查看以下链接。这已经回答了:

    https://wordpress.stackexchange.com/a/235874/87704

    【讨论】:

      【解决方案3】:

      您可以使用post_limits 挂钩来限制产品类别存档页面上显示的产品总数:

      add_filter( 'post_limits', 'product_category_archives_limit', 10, 2 );
      function product_category_archives_limit( $limit, $query ) {
          if ( is_product_category() ) {
              $limit = 'LIMIT 1000';
          }
          return $limit;
      }
      

      代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

      相关:Limit posts on a WP_Query and in WooCommerce product query

      【讨论】:

        猜你喜欢
        • 2019-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-14
        • 1970-01-01
        相关资源
        最近更新 更多