从自定义商店页面上的 woocommerce 循环中删除类别
Remove category from woocommerce loop on custom shop page
我在自定义商店页面上使用此代码进行产品循环:
<?php
$product = new WC_Product(get_the_ID());
$params = array('posts_per_page' => 12, 'post_type' =>'product');
$wc_query = new WP_Query($params);
?>
<?php if ($wc_query->have_posts()) : ?>
<?php while ($wc_query->have_posts()) :
$wc_query->the_post(); ?>
<article class="portfolio__item portfolio__item--shop">
<figure class="blog__image-container">
<?php if ( has_post_thumbnail()) {the_post_thumbnail('thumb-front' ,array("class"=>"portfolio__image post_thumbnail"));} ?>
</figure>
<h3 class="portfolio__content-title portfolio__content-title--shop"><?php the_title(); ?></h3>
<p class="portfolio__content-text portfolio__content-text--shop"><?php $product = new WC_Product(get_the_ID()); echo $product->get_price_html(); ?></p>
<a href="?add-to-cart=<?php echo $product->id; ?>" class="portfolio__link">
<div class="portfolio__content">
<p class="portfolio__content-text">Click to buy</p>
</div>
</a>
</article>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p>
<?php _e( 'No Products'); ?>
</p>
<?php endif; ?>
我想从这个循环中排除一个类别。我正在尝试此代码
add_action( 'pre_get_posts', 'remove_cat_from_shop_loop' );
function remove_cat_from_shop_loop( $q ) {
if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
if ( ! is_admin() && is_shop() ) {
$q->set( 'tax_query', array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'free' ), // Change it to the slug you want to hide
'operator' => 'NOT IN'
)));
}
remove_action( 'pre_get_posts', 'remove_cat_from_shop_loop' );
}
但这对我不起作用。再等一会。当我也尝试在管理面板中添加新类别时出现未定义错误,但刷新页面后存在新类别。我的页面 http://test.art-electrik.ru/wrap/dark/wordpress/shop/
您应该尝试在循环中使用 WordPress 条件函数 has_term()
。
因此您的代码将是:
<?php
// Set here the category id, slug or name to be removed
$removed_category = 'my_category';
$product = new WC_Product(get_the_ID());
$params = array('posts_per_page' => 12, 'post_type' =>'product');
$wc_query = new WP_Query($params);
?>
<?php if ($wc_query->have_posts()) : ?>
<?php while ($wc_query->have_posts()) :
$wc_query->the_post(); ?>
<?php if ( !has_term( $removed_category, 'product_cat' ) ): // <== <== <== ## HERE ## ?>
<article class="portfolio__item portfolio__item--shop">
<figure class="blog__image-container">
<?php if ( has_post_thumbnail()) {the_post_thumbnail('thumb-front' ,array("class"=>"portfolio__image post_thumbnail"));} ?>
</figure>
<h3 class="portfolio__content-title portfolio__content-title--shop"><?php the_title(); ?></h3>
<p class="portfolio__content-text portfolio__content-text--shop"><?php $product = new WC_Product(get_the_ID()); echo $product->get_price_html(); ?></p>
<a href="?add-to-cart=<?php echo $product->id; ?>" class="portfolio__link">
<div class="portfolio__content">
<p class="portfolio__content-text">Click to buy</p>
</div>
</a>
</article>
<?php endif; ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p>
<?php _e( 'No Products'); ?>
</p>
<?php endif; ?>
这应该有效。
由于您有自定义循环,因此无需使用 pre_get_posts 过滤器。只需将您想要的代码添加到 WP_Query 参数即可。如果您仍想使用过滤器,则需要改用 parse_tax_query 过滤器。由于 pre_get_posts 触发得太晚,无法进行适当的税务查询调整。因此,您的自定义查询参数将如下所示:
$params = array(
'posts_per_page' => 12,
'post_type' =>'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'free' ),
'operator' => 'NOT IN'
)
)
);
$wc_query = new WP_Query( $params );
我在自定义商店页面上使用此代码进行产品循环:
<?php
$product = new WC_Product(get_the_ID());
$params = array('posts_per_page' => 12, 'post_type' =>'product');
$wc_query = new WP_Query($params);
?>
<?php if ($wc_query->have_posts()) : ?>
<?php while ($wc_query->have_posts()) :
$wc_query->the_post(); ?>
<article class="portfolio__item portfolio__item--shop">
<figure class="blog__image-container">
<?php if ( has_post_thumbnail()) {the_post_thumbnail('thumb-front' ,array("class"=>"portfolio__image post_thumbnail"));} ?>
</figure>
<h3 class="portfolio__content-title portfolio__content-title--shop"><?php the_title(); ?></h3>
<p class="portfolio__content-text portfolio__content-text--shop"><?php $product = new WC_Product(get_the_ID()); echo $product->get_price_html(); ?></p>
<a href="?add-to-cart=<?php echo $product->id; ?>" class="portfolio__link">
<div class="portfolio__content">
<p class="portfolio__content-text">Click to buy</p>
</div>
</a>
</article>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p>
<?php _e( 'No Products'); ?>
</p>
<?php endif; ?>
我想从这个循环中排除一个类别。我正在尝试此代码
add_action( 'pre_get_posts', 'remove_cat_from_shop_loop' );
function remove_cat_from_shop_loop( $q ) {
if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
if ( ! is_admin() && is_shop() ) {
$q->set( 'tax_query', array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'free' ), // Change it to the slug you want to hide
'operator' => 'NOT IN'
)));
}
remove_action( 'pre_get_posts', 'remove_cat_from_shop_loop' );
}
但这对我不起作用。再等一会。当我也尝试在管理面板中添加新类别时出现未定义错误,但刷新页面后存在新类别。我的页面 http://test.art-electrik.ru/wrap/dark/wordpress/shop/
您应该尝试在循环中使用 WordPress 条件函数 has_term()
。
因此您的代码将是:
<?php
// Set here the category id, slug or name to be removed
$removed_category = 'my_category';
$product = new WC_Product(get_the_ID());
$params = array('posts_per_page' => 12, 'post_type' =>'product');
$wc_query = new WP_Query($params);
?>
<?php if ($wc_query->have_posts()) : ?>
<?php while ($wc_query->have_posts()) :
$wc_query->the_post(); ?>
<?php if ( !has_term( $removed_category, 'product_cat' ) ): // <== <== <== ## HERE ## ?>
<article class="portfolio__item portfolio__item--shop">
<figure class="blog__image-container">
<?php if ( has_post_thumbnail()) {the_post_thumbnail('thumb-front' ,array("class"=>"portfolio__image post_thumbnail"));} ?>
</figure>
<h3 class="portfolio__content-title portfolio__content-title--shop"><?php the_title(); ?></h3>
<p class="portfolio__content-text portfolio__content-text--shop"><?php $product = new WC_Product(get_the_ID()); echo $product->get_price_html(); ?></p>
<a href="?add-to-cart=<?php echo $product->id; ?>" class="portfolio__link">
<div class="portfolio__content">
<p class="portfolio__content-text">Click to buy</p>
</div>
</a>
</article>
<?php endif; ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p>
<?php _e( 'No Products'); ?>
</p>
<?php endif; ?>
这应该有效。
由于您有自定义循环,因此无需使用 pre_get_posts 过滤器。只需将您想要的代码添加到 WP_Query 参数即可。如果您仍想使用过滤器,则需要改用 parse_tax_query 过滤器。由于 pre_get_posts 触发得太晚,无法进行适当的税务查询调整。因此,您的自定义查询参数将如下所示:
$params = array(
'posts_per_page' => 12,
'post_type' =>'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'free' ),
'operator' => 'NOT IN'
)
)
);
$wc_query = new WP_Query( $params );