2013-03-11 154 views
2

我试图在woo comm插件中找到代码(短代码),该代码使得所有产品的列表都可以从一个类别中修改成... 1小时后没有运气,仍然没有找到。WooCommerce Custom Loop从一个特定类别获取所有产品

所以我开始编码它自己(重新发明轮子),在这里我试图让

让我所有的产品从类别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; 
} 

回答

8

这里是我的代码已经找到,并修改我的需求

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

} 

?> 
+1

什么是'atts',这段代码应该返回什么?所有的类别? – Tester 2014-01-08 23:26:52

4

/woocommerce/includes/class-wc-shortcodes.php定义的[product_category]短代码是一个很好的起点,特别是Woocommerce不断发展!

它的胆量只是一个标准的WP_Query,增加了一些额外的代码来做分页,设置Woocommerce设置的排序顺序,以及一些检查产品是否被标记为可见或不可见。

所以,如果你剔除短码相关的代码,想要的功能,只是得到了明显的产品与特定塞一类,它应该是这样的:

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

传中,蛞蝓和你”将使用您在Woocommerce设置中配置的相同顺序取回标准WordPress发布内容。

3

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

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

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

相关问题