首页 > 解决方案 > 在 WooCommerce 类别档案中仅在第 6 个产品之后插入一次 DIV

问题描述

我想在一个产品类别的第五个产品之后显示一些内容。

基于在 WooCommerce 存档答案代码中的产品行之间添加内容,我正在使用这个稍微修改过的版本代码:

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;
}

这种工作,但多次添加自定义内容(DIV)。它每 6 列添加一次。在第 6 列之后,我只需要一个 DIV。

标签: phpwordpresswoocommerceproductcategories

解决方案


如果您想在第六列之后只显示一次内容,只需将 if 条件更改为:

if ($current_post == 6) :

推荐阅读