在 WooCommerce 档案中的产品行之间添加内容
Add content in between product rows in WooCommerce archives
我想在某个产品类别的第三个产品(可能是第六个、第九个...)之后展示一些内容。并非每个类别都有额外的内容或相同数量的内容。所以它应该是灵活的。
我发现 an example 使用以下代码:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'template-parts/content' ); ?>
<?php if ( $wp_query->current_post == 1 ) { ?>
<div>Put Ad Here</div>
<?php } ?>
<?php endwhile; endif; ?>
我将该代码添加到我的 archive-product.php
中,如下所示:
if ( wc_get_loop_prop( 'total' ) ) {
while ( have_posts() ) {
the_post();
/**
* Hook: woocommerce_shop_loop.
*/
do_action( 'woocommerce_shop_loop' );
wc_get_template_part( 'content', 'product' );
if ( $wp_query->current_post == 1 ) {
echo '<div>Put Ad Here</div>';
}
}
}
但它没有显示任何内容。
如果有一种方法可以在不触及模板文件的情况下添加这些内容,那就太好了。
有没有我可以使用的钩子?
已更新 - 您可以使用以下挂钩函数,而不是覆盖模板文件,这将在每个产品行之间添加自定义内容整行:
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 > 1 ) :
?>
</ul>
<ul class="columns-1" style="list-style:none; margin:0 0 3em;">
<li style="text-align:center; padding:2em 1em; border: solid 1px #ccc;"><div class="banner"><?php _e("Custom content here"); ?></div></li>
</ul>
<ul class="products columns-<?php echo $columns; ?>">
<?php
endif; endif;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
由于我的菜鸟身份无法发表评论,但是如果您更改上面的代码,可以添加一个小的内容
if ( ( $current_post % $columns ) == 0 && $current_post > 1 )
至
if ( ( $current_post % $columns ) == 0 && $current_post%6==0 )
内容将放在每第6个产品之后。显然,您可以使用任何数字。
认为这很有帮助,因为我找不到解决方案。
我想在某个产品类别的第三个产品(可能是第六个、第九个...)之后展示一些内容。并非每个类别都有额外的内容或相同数量的内容。所以它应该是灵活的。
我发现 an example 使用以下代码:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'template-parts/content' ); ?>
<?php if ( $wp_query->current_post == 1 ) { ?>
<div>Put Ad Here</div>
<?php } ?>
<?php endwhile; endif; ?>
我将该代码添加到我的 archive-product.php
中,如下所示:
if ( wc_get_loop_prop( 'total' ) ) {
while ( have_posts() ) {
the_post();
/**
* Hook: woocommerce_shop_loop.
*/
do_action( 'woocommerce_shop_loop' );
wc_get_template_part( 'content', 'product' );
if ( $wp_query->current_post == 1 ) {
echo '<div>Put Ad Here</div>';
}
}
}
但它没有显示任何内容。 如果有一种方法可以在不触及模板文件的情况下添加这些内容,那就太好了。
有没有我可以使用的钩子?
已更新 - 您可以使用以下挂钩函数,而不是覆盖模板文件,这将在每个产品行之间添加自定义内容整行:
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 > 1 ) :
?>
</ul>
<ul class="columns-1" style="list-style:none; margin:0 0 3em;">
<li style="text-align:center; padding:2em 1em; border: solid 1px #ccc;"><div class="banner"><?php _e("Custom content here"); ?></div></li>
</ul>
<ul class="products columns-<?php echo $columns; ?>">
<?php
endif; endif;
}
代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。
由于我的菜鸟身份无法发表评论,但是如果您更改上面的代码,可以添加一个小的内容
if ( ( $current_post % $columns ) == 0 && $current_post > 1 )
至
if ( ( $current_post % $columns ) == 0 && $current_post%6==0 )
内容将放在每第6个产品之后。显然,您可以使用任何数字。 认为这很有帮助,因为我找不到解决方案。