2013-03-11 166 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--> 

使用上面的代碼意味着你得到默認的內聯樣式,類和其他必要的標記。

相關問題