简体   繁体   English

如何为自定义商店页面制作 woocommerce 产品列

[英]How to make woocommerce products columns for custom shop page

I am looking for column argument for WP_Query() .我正在寻找WP_Query()的列参数。 As the post type will be product .因为帖子类型将是product See the code below:请看下面的代码:

$args = array(
    'post_type' => 'product',
    'posts_per_page' => 8,
    'columns' => 3, // Can we have something like this
);
$loop = new \WP_Query( $args );
if ( $loop->have_posts() ) {  
    while ( $loop->have_posts() ) : $loop->the_post(); 
        wc_get_template_part( 'content', 'product' );
    endwhile;
} else {
    echo esc_html__( 'No products found', 'theme_core' );
}
wp_reset_postdata();

Can we have a column argument to get products per row?我们可以有一个列参数来获取每行的产品吗?

For that, you have to put the row for 3 products like the below code.为此,您必须将 3 种产品的行放在下面的代码中。

    $args = array(
    'post_type' => 'product',
    'posts_per_page' => 8
);
$product = 1;
$total_product = get_products_count(); // Total Product
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {  ?>
    <div class="row">
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
        <?php wc_get_template_part( 'content', 'product' ); ?>
        <?php if( $product % 3 == 0 ) { ?>
            </div>
        <?php } else { ?>
            <?php if( $product == $total_product ) { ?>
                </div>
            <?php } ?>
        <?php } ?>
    <?php endwhile; $product++;
} else {
    echo esc_html__( 'No products found', 'theme_core' );
}
wp_reset_postdata();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM