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

时间:2016-10-23 15:50:55

标签: php wordpress woocommerce categories product

我在自定义商店页面上使用此代码进行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; ?>

我希望从这个循环中排除一个类别。我正在尝试这段代码

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/

2 个答案:

答案 0 :(得分:2)

由于您确实有自定义循环,因此无需使用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 );

答案 1 :(得分:1)

您应该尝试在循环中使用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; ?>

这应该有用。

相关问题