Woocommerce 自定义产品循环 Post 每页问题

Woocommerce Custom Product Loop Post Per Page Issue

我使用“WP_Query”创建了一个自定义产品循环,以在 Table 中显示 Woocommerce 产品数据,并且工作正常。但是 'posts_per_page' => -1, 并未显示示例类别中的所有产品。

目前我在样本类别中有 17 个产品。它只显示 10 种产品。 我检查了问题,发现帖子中使用了“WP_Query”,所以 它每页的价值 Post 来自 设置>阅读>博客页面最多显示

如何在不更改 Wordpress Post 每页设置的情况下使 'posts_per_page' => -1 获取超过 10 个产品。 以下是我的完整代码。

<?php // The args for the loop
    $args = array(
        'posts_per_page' => -1,
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'product_cat',
                'field' => 'slug',
                'terms' => 'sample' // Your category name here
            )
        ),
        'post_type' => 'product',
        'orderby' => 'title',
    ); ?>
    <?php
        $loop = new WP_Query($args); // The Loop
        if ( $loop->have_posts() ) {
            while ( $loop->have_posts() ) : $loop->the_post(); ?>
        <table> <!-- Fetching woocommerce data in table -->
        <tr>
          <td><?php the_content(); ?></td> <!-- Content -->
          <td><?php the_title(); ?></td> <!-- Title -->
          <td><?php echo ( $sku = $product->get_sku() ) ? $sku : __( 'N/A', 'woocommerce' ); ?></td> <!-- SKU -->
          <td><?php   echo $product->get_price_html(); ?></td> <!-- Price -->
          <td>
           <?php global $product; // For Adding Add to Cart button in loop
              echo apply_filters( 'woocommerce_loop_add_to_cart_link',
              sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" class="button %s product_type_%s">%s</a>',
              esc_url( $product->add_to_cart_url() ),
              esc_attr( $product->id ),
              esc_attr( $product->get_sku() ),
              $product->is_purchasable() ? 'add_to_cart_button' : '',
              esc_attr( $product->product_type ),
              esc_html( $product->add_to_cart_text() )
              ), $product ); 
            ?>
        </td>
        </tr>
        </table>
        <?php
            endwhile;
        } else {
            echo __( 'No products found' );
        }
        wp_reset_postdata();
    ?>

请使用以下代码

'posts_per_page' => -1,

而不是这个

$maxposts = get_option('posts_per_page');
'posts_per_page' => $maxposts,

试试下面的代码片段

add_filter( 'loop_shop_per_page', create_function( '$cols', 'return 30;' ), 20 );

参考 http://www.boopathirajan.com/increasechange-product-per-page-limit-woocommerce-product-loop/

WooCommerce 使用与博客文章相同的 "posts per page" 设置。

可以这样修改

add_filter( 'loop_shop_per_page', 'so_27395967_products_per_page' );
function so_27395967_products_per_page(){
    return 12;
}