Take all produts by category

时间:2021-11-08 作者:Digital Point

我不知道为什么我不能按类别名称取产品。我找不到任何产品。代码如下所示

 <ul class="glide__slides">
<?php
    $args = array(
        \'post_type\'      => \'product\',
        \'category_name\' => \'trofee\',
        \'posts_per_page\' => 20,
        \'meta_query\'     => array(
                \'relation\' => \'OR\',
                array( // Simple products type
                    \'key\'           => \'_sale_price\',
                    \'value\'         => 0,
                    \'compare\'       => \'>\',
                    \'type\'          => \'numeric\'
                ),
                array( // Variable products type
                    \'key\'           => \'_min_variation_sale_price\',
                    \'value\'         => 0,
                    \'compare\'       => \'>\',
                    \'type\'          => \'numeric\'
                )
            )
    );
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post();
        global $product;
        $id = $product->get_id();
        echo \'<li class="glide__slide">\';
        echo \'<br /><a href="\'.get_permalink().\'">\' . woocommerce_get_product_thumbnail().\' <br> \'.get_the_title().   \'<br>\' . \'<div class="bf-sale"><span>\' . $product->get_regular_price() .\' Lei </span>\' . \'<span>\' . $product->get_sale_price()  .\'Lei</span></div>\' .\'</a>\';
        echo \'<div class="trophies-container__trophy-action">\';
        echo sprintf(\' <a href="?add-to-cart=%d data-quantity="1" class="button product_type_simple add_to_cart_button ajax_add_to_cart" data-product_id="%d" data-product_sku=""  rel="nofollow"><i class="fas fa-shopping-cart"></i>Adaugă în coș</a>\',$id,$id);
        echo \'</div>\';
        echo \'</li>\';
        endwhile;
    } else {
        echo __( \'No products found\' );
    }
    wp_reset_postdata();
?>

1 个回复
SO网友:Buttered_Toast

category_name 对于类别分类,sense产品使用product_cat 您需要添加tax_query 进入$args.
像这样。

$args = array(
    \'post_type\'      => \'product\',
    \'posts_per_page\' => 20,
    \'tax_query\' => [
        \'taxonomy\' => \'product_cat\',
        \'field\'    => \'name\',
        \'terms\'    => \'trofee\',
    ],
    \'meta_query\'     => array(
        \'relation\' => \'OR\',
        array( // Simple products type
            \'key\'           => \'_sale_price\',
            \'value\'         => 0,
            \'compare\'       => \'>\',
            \'type\'          => \'numeric\'
        ),
        array( // Variable products type
            \'key\'           => \'_min_variation_sale_price\',
            \'value\'         => 0,
            \'compare\'       => \'>\',
            \'type\'          => \'numeric\'
        )
    )
);

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post