'Insert a DIV after the 6th product only once in WooCommerce category archives
I would like to show some content after the fifth product of a product category.
Based on Add content in between product rows in WooCommerce archives answers code, I am using this slight modified version code:
add_action( 'woocommerce_shop_loop', 'action_woocommerce_shop_loop', 100 );
function action_woocommerce_shop_loop() {
// Only on producy cayegory archives
if ( is_product_category() ) :
global $wp_query;
// Get the number of columns set for this query
$columns = esc_attr( wc_get_loop_prop( 'columns' ) );
// Get the current post count
$current_post = $wp_query->current_post;
if ( ( $current_post % $columns ) == 0 && $current_post%6==0 ) :
?>
<div class="product-grid-item col-md-3 col-sm-4 col-xs-6"><div class="banner"><?php _e("Custom content here"); ?></div></div>
<?php
endif; endif;
}
This kind of works, but adds the custom content (DIVs) multiple times. It's adding it every 6 columns. I just need one single DIV after the 6th column.
Solution 1:[1]
If you want to display the content just once after sixth column, just change the if condition to:
if ($current_post == 6) :
Solution 2:[2]
You can also insert the content after any number of posts by using this code:
add_action( 'woocommerce_shop_loop', 'action_woocommerce_shop_loop', 100 );
function action_woocommerce_shop_loop() {
if ( is_product_category() ) :
global $wp_query;
$columns = esc_attr( wc_get_loop_prop( 'columns' ) );
$current_post = $wp_query->current_post;
if ( ( $current_post % $columns ) == 0 && (in_array($current_post, array (6, 10, 45)))) :
?>
</ul>
<ul class="columns-1" style="list-style:none; margin:0 0 3em;">
<li style=""><div class="banner">
<!--Insert Ads here -->
</div></li>
</ul>
<ul class="products columns-<?php echo $columns; ?>">
<?php
endif;endif;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | shahidiqbal |
Solution 2 | Denny |