简体   繁体   English

从自定义商店页面上的woocommerce循环中删除类别

[英]Remove category from woocommerce loop on custom shop page

I'm using this code for prodcut loop on custom shop page: 我在自定义商店页面上将此代码用于prodcut循环:

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

And i want exclude one category from this loop. 我想从此循环中排除一个类别。 I'm trying this code 我正在尝试此代码

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' );


}

But it doesn't work for me. 但这对我不起作用。 And one more moment. 还有一刻。 When i'm trying too add new category in admin panel i have undefined error, but after refreshing page new category exist. 当我也尝试在管理面板中添加新类别时,出现未定义的错误,但刷新页面后存在新类别。 My page http://test.art-electrik.ru/wrap/dark/wordpress/shop/ 我的页面http://test.art-electrik.ru/wrap/dark/wordpress/shop/

Since you do have a custom loop there's no need to use the pre_get_posts filter. 由于您确实有一个自定义循环,因此无需使用pre_get_posts过滤器。 Just add the code you want to your WP_Query parameters. 只需将所需的代码添加到WP_Query参数即可。 If you still want to use the filter, you'll need to use the parse_tax_query filter instead. 如果仍然要使用过滤器,则需要使用parse_tax_query过滤器。 As the pre_get_posts is fired too late to make a proper tax query adjustments. 由于pre_get_posts被解雇为时已晚,因此无法进行适当的税收查询调整。 So your parameters, for your custom query, will look like this: 因此,用于自定义查询的参数将如下所示:

$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 );

You should try to use in your loop, the WordPress conditional function has_term() . 您应该尝试在循环中使用WordPress条件函数has_term()

So your code will be: 因此,您的代码将是:

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

This should work. 这应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM