在/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發佈內容。
什麼是'atts',這段代碼應該返回什麼?所有的類別? – Tester 2014-01-08 23:26:52