Arm*_*cía 6 php wordpress product featured woocommerce
我将WooCommerce更新到版本3.0但我无法在我的主题上显示特色产品,我google了一段时间并让WC删除了_feature并将其添加到分类中.但我不太了解我的主题如何获得特色产品.
这是错误的特色产品的代码.
$meta_query = WC()->query->get_meta_query();
$meta_query[] = array(
'key' => '_featured',
'value' => 'yes'
);
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $products,
'orderby' => $orderby,
'order' => $order == 'asc' ? 'asc' : 'desc',
'meta_query' => $meta_query
);
Run Code Online (Sandbox Code Playgroud)
如果您知道DataBase中的特色项目在哪里.非常感谢.
Loi*_*tec 17
自Woocommerce 3以来,您需要使用Tax Query,因为特色产品现在由该术语的product_visibility 自定义分类处理featured:
// The tax query
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN', // or 'NOT IN' to exclude feature products
);
// The query
$query = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $products,
'orderby' => $orderby,
'order' => $order == 'asc' ? 'asc' : 'desc',
'tax_query' => $tax_query // <===
) );
Run Code Online (Sandbox Code Playgroud)
参考文献:
您可以使用
wc_get_featured_product_ids()函数来获取特色产品ID数组,但在a中使用税务查询WP_Query就可以了,并且正确...
有关:
它应该有效.
Fel*_*lia 10
这是一个老问题,但您也可以使用wc_get_featured_product_ids():
$args = array(
'post_type' => 'product',
'posts_per_page' => $products,
'orderby' => $orderby,
'order' => $order == 'asc' ? 'asc' : 'desc',
'post__in' => wc_get_featured_product_ids(),
);
$query = new WP_Query( $args );
Run Code Online (Sandbox Code Playgroud)
刚刚在这里发现的。我希望它有帮助!
小智 5
$args = array(
'post_type' => 'product',
'posts_per_page' => 12,
'tax_query' => array(
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
),
),
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
Run Code Online (Sandbox Code Playgroud)
您现在可以使用 wc_get_products 并将参数 features 设置为 true。见https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query
$args = array(
'featured' => true,
);
$products = wc_get_products( $args );
Run Code Online (Sandbox Code Playgroud)
对于按类别寻找特色产品的人,您可以查看我关于此的注释 => https://jameshwartlopez.com/plugin/get-featured-products-of-a-category/