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