【问题标题】:WooCommerce Custom Loop to get all product from one specific categoryWooCommerce 自定义循环从一个特定类别中获取所有产品
【发布时间】:2013-02-26 03:53:24
【问题描述】:

我尝试在 woo comm 插件中找到列出一个类别的所有产品列表的代码(短代码),以便能够对其进行修改...... 1 小时后没有运气,仍然没有找到。

所以我开始自己编写代码(重新发明轮子),这就是我想要得到的东西

从类别 ID="151" 中获取所有产品,并能够输出名称、永久链接等...

这是现在的代码,它返回一切......太多了!我不知道如何过滤它

{
$args = array(
    'post_type' => 'product',
    'posts_per_page' => 99
);

$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
    //echo get_title()."<br/>";
    var_dump($loop);
endwhile;
} 

【问题讨论】:

    标签: filter categories woocommerce


    【解决方案1】:

    这是我找到的代码,并根据我的需要进行修改

    function get_me_list_of($atts, $content = null)
    {   
        $args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'product_cat' => $atts[0], 'orderby' => 'rand' );
    
        $loop = new WP_Query( $args );
    
        echo '<h1 class="upp">Style '.$atts[0].'</h1>';
        echo "<ul class='mylisting'>";
        while ( $loop->have_posts() ) : $loop->the_post(); 
        global $product; 
    
        echo '<li><a href="'.get_permalink().'">'.get_the_post_thumbnail($loop->post->ID, 'thumbnail').'</a></li>';
    
        endwhile; 
    
        echo "</ul>";
    
        wp_reset_query(); 
    
    }
    
    ?>
    

    【讨论】:

    • atts 是什么,这段代码应该返回什么?所有类别?
    【解决方案2】:

    /woocommerce/includes/class-wc-shortcodes.php 中定义的 [product_category] 短代码是一个很好的起点,尤其是在 Woocommerce 不断发展的情况下!

    它的核心只是一个标准的 WP_Query,添加了一些额外的代码来进行分页,从 Woocommerce 设置中设置排序顺序,并检查产品是否标记为可见。

    因此,如果您去掉与短代码相关的代码,并想要一个从具有特定 slug 的类别中获取可见产品的函数,它看起来像这样:

    function getCategoryProducts($category_slug) {
        // Default Woocommerce ordering args
        $ordering_args = WC()->query->get_catalog_ordering_args();
    
        $args = array(
                'post_type'             => 'product',
                'post_status'           => 'publish',
                'ignore_sticky_posts'   => 1,
                'orderby'               => $ordering_args['orderby'],
                'order'                 => $ordering_args['order'],
                'posts_per_page'        => '12',
                'meta_query'            => array(
                    array(
                        'key'           => '_visibility',
                        'value'         => array('catalog', 'visible'),
                        'compare'       => 'IN'
                    )
                ),
                'tax_query'             => array(
                    array(
                        'taxonomy'      => 'product_cat',
                        'terms'         => array( esc_attr( $category_slug ) ),
                        'field'         => 'slug',
                        'operator'      => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
                    )
                )
            );
    
        if ( isset( $ordering_args['meta_key'] ) ) {
                $args['meta_key'] = $ordering_args['meta_key'];
        }
        $products = new WP_Query($args);
    
        woocommerce_reset_loop();
        wp_reset_postdata();
    
        return $products;
    }
    

    传入 slug,您将使用您在 Woocommerce 设置中配置的相同顺序返回标准的 wordpress 帖子集合。

    【讨论】:

      【解决方案3】:

      我有类似的问题,因为我想为“自定义页面”获取“Fabrics”产品,这是我使用的代码。

      <ul class="products">
      <?php
          $args = array(
              'post_type' => 'product',
              'posts_per_page' => 12,
              'product_cat' => 'fabrics'
              );
          $loop = new WP_Query( $args );
          if ( $loop->have_posts() ) {
              while ( $loop->have_posts() ) : $loop->the_post();
                  woocommerce_get_template_part( 'content', 'product' );
              endwhile;
          } else {
              echo __( 'No products found' );
          }
          wp_reset_postdata();
      ?>
      </ul><!--/.products-->
      

      使用上面的代码意味着你得到了默认的内联样式、类和其他必要的标签。

      【讨论】:

      • 请告诉我类别随机
      猜你喜欢
      • 2021-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-24
      • 2016-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多