I am looking for column argument for WP_Query()
. As the post type will be 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.
$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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.