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

标签 php wordpress woocommerce categories product

我在自定义商店页面上使用此代码进行产品循环:

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

最佳答案

由于您确实有自定义循环,因此无需使用 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 - 从自定义商店页面上的 woocommerce 循环中删除类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40204768/

相关文章:

wordpress - Ionic 3 和 Woocommerce - 使用 woocommerce 客户 api 检索/创建客户?

javascript - 使用 AJAX - WooCommerce API 向购物车添加变体?

wordpress - 如果项目使用统一费率,则删除 "Calculate Shipping"按钮

php - WooCommerce 管理员订单编辑保存帖子

php - 打破关于mysql的脚本错误

PHP - error_log() 不会打印到文件

php - 具有相同 slug 的自定义帖子重定向到具有相同 slug 的错误帖子

php - 在屏幕上回显 MySQL 列值

php - 使用 comet 和 PHP 进行实时数据更新?

php - wordpress 如何将 X-FRAME 限制为同源?