通过woocommerce在模板页面上的自定义循环显示特色产品
-
-
从var_dump(get_meta_values('_featured','product');`中添加结果;其中,get_meta_values函数由[this answer](http://wordpress.stackexchange.com/a/9451/31545)Add the result from `var_dump( get_meta_values( '_featured', 'product' );` where the function `get_meta_values` is supported by the custom function explained in [this answer](http://wordpress.stackexchange.com/a/9451/31545)
- 0
- 2015-07-24
- Pieter Goosen
-
9 个回答
- 投票数
-
- 2015-09-15
将您的参数更改为这样:
$meta_query = WC()->query->get_meta_query(); $meta_query[] = array( 'key' => '_featured', 'value' => 'yes' ); $args = array( 'post_type' => 'product', 'stock' => 1, 'showposts' => 6, 'orderby' => 'date', 'order' => 'DESC', 'meta_query' => $meta_query );
如果您访问wp-content/plugins/woocommerce/includes/class-wc-shortcodes.php(@ 595),则可以找到WC短代码的处理方法.
Change your args to be like this:
$meta_query = WC()->query->get_meta_query(); $meta_query[] = array( 'key' => '_featured', 'value' => 'yes' ); $args = array( 'post_type' => 'product', 'stock' => 1, 'showposts' => 6, 'orderby' => 'date', 'order' => 'DESC', 'meta_query' => $meta_query );
If you go to wp-content/plugins/woocommerce/includes/class-wc-shortcodes.php (@595) you can find how it's done for WC shortcodes.
-
他们要注意的关键是" _featured"未存储为数值.它存储为字符串"是"或"否".OP问题中的其他所有内容都应该对我有用.They key to note is that '_featured' is not stored as a numeric value. It is stored as a string 'yes' or 'no'. Everything else in the OP question should work, worked for me.
- 3
- 2016-07-03
- i_a
-
从WooCommerce 3.0开始,此解决方案不再起作用.请在下面查看我更新的答案.As of WooCommerce 3.0, this solution no longer works. Please see my updated answer below.
- 1
- 2018-06-12
- dpruth
-
- 2017-05-08
这在WooCommerce 3.0中已更改.它不仅是meta_query,而且现在包括tax_query.现在的参数是:
$meta_query = WC()->query->get_meta_query(); $tax_query = WC()->query->get_tax_query(); $tax_query[] = array( 'taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'featured', 'operator' => 'IN', ); $args = array( 'post_type' => 'product', 'post_status' => 'publish', 'ignore_sticky_posts' => 1, 'posts_per_page' => $atts['per_page'], 'orderby' => $atts['orderby'], 'order' => $atts['order'], 'meta_query' => $meta_query, 'tax_query' => $tax_query, );
请参阅woocommerce/includes/class-wc-shortcodes.php
This has changed in WooCommerce 3.0. It's not simply a meta_query, but now includes a tax_query. The arguments are now:
$meta_query = WC()->query->get_meta_query(); $tax_query = WC()->query->get_tax_query(); $tax_query[] = array( 'taxonomy' => 'product_visibility', 'field' => 'name', 'terms' => 'featured', 'operator' => 'IN', ); $args = array( 'post_type' => 'product', 'post_status' => 'publish', 'ignore_sticky_posts' => 1, 'posts_per_page' => $atts['per_page'], 'orderby' => $atts['orderby'], 'order' => $atts['order'], 'meta_query' => $meta_query, 'tax_query' => $tax_query, );
See woocommerce/includes/class-wc-shortcodes.php
-
正是我想要的!Just what I was looking for!
- 1
- 2018-05-10
- joshkrz
-
同样对于Woocommerce 3.0,他们建议使用" wc_placeholder_img_src"而不是" woocommerce_placeholder_img_src".Also for Woocommerce 3.0, they recommend using `wc_placeholder_img_src` instead of `woocommerce_placeholder_img_src`.
- 0
- 2018-05-25
- Robotnicka
-
- 2017-12-24
WooCommerce 3中的特色产品循环
<ul class="products"> <?php $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(); ?>
Featured Products Loop in WooCommerce 3
<ul class="products"> <?php $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(); ?>
-
- 2018-05-24
根据 WooCommerce Wiki :
在将来的WooCommerce版本中,随着数据移向自定义表以提高性能,构建自定义WP_Queries或数据库查询(以检索产品)可能会破坏代码.
WooCommerce提倡使用
wc_get_products()
或WC_Product_Query()
而不是WP_Query()
或get_posts()
我已经用我用来实现您想要的代码的代码写了一篇文章: https://cfxdesign.com/create-a-custom-woocommerce-product-loop-the-right-way/
According to the WooCommerce Wiki:
Building custom WP_Queries or database queries [to retrieve products] is likely to break your code in future versions of WooCommerce as data moves towards custom tables for better performance.
WooCommerce advocates using
wc_get_products()
orWC_Product_Query()
instead ofWP_Query()
orget_posts()
.I've written a post with the code I used to achieve what you want here: https://cfxdesign.com/create-a-custom-woocommerce-product-loop-the-right-way/
-
抱歉,没有看到任何书面代码,很难理解您的文章.您能提供一些编码吗?sorry, without seeing some written code, it is hard to understand your article. Can you please include some coding ?
- 0
- 2019-06-05
- HOY
-
@HOY嵌入插件已损坏;现在已修复,您可以看到代码!@HOY the embed plugin was broken; it's fixed now and you can see the code!
- 0
- 2019-06-05
- cfx
-
谢谢,在寻找解决方案时,我提出了以下解决方案.我不确定它与您的产品有何不同,因为我无法彻底检查您的产品,但是它很短,可以帮助我进行自定义产品循环.https://www.kathyisawesome.com/woocommerce-modifying-product-query/thank you, while looking for solutions, I came up with this one below. I am not sure how it differs from yours since I was not able to check yours throughly but it is very short and helped me with a custom product loop. https://www.kathyisawesome.com/woocommerce-modifying-product-query/
- 0
- 2019-06-05
- HOY
-
- 2018-04-28
我知道这已经很老了,但是我刚刚在此处分享了一个替代解决方案,我认为它可以为您提供帮助那些也达到这个主题的人.
您可以使用 wc_get_featured_product_ids()也是如此:
$args = array( 'post_type' => 'product', 'posts_per_page' => 6, 'orderby' => 'date', 'order' => 'DESC', 'post__in' => wc_get_featured_product_ids(), ); $query = new WP_Query( $args );
希望对您有帮助!
I know this is quite old, but I've just shared an alternative solution here and I think it can help those reaching this topic too.
Instead of using
meta_query
ortax_query
, you can use wc_get_featured_product_ids() too:$args = array( 'post_type' => 'product', 'posts_per_page' => 6, 'orderby' => 'date', 'order' => 'DESC', 'post__in' => wc_get_featured_product_ids(), ); $query = new WP_Query( $args );
I hope it helps!
-
- 2018-09-26
基于: https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query
我会尝试:
外循环:
$args = array ( 'limit' => 6, 'orderby' => 'title', 'order' => 'ASC', 'category' => $club_cat, 'stock_status' => 'instock', 'featured' => true, ); $products = wc_get_products( $args );
在循环中:
$query = new WC_Product_Query( array( 'limit' => 6, 'orderby' => 'title', 'order' => 'ASC', 'category' => $club_cat, 'stock_status' => 'instock', 'featured' => true, 'return' => 'ids', ) ); $products = $query->get_products();
Based on: https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query
I would try:
outside loop:
$args = array ( 'limit' => 6, 'orderby' => 'title', 'order' => 'ASC', 'category' => $club_cat, 'stock_status' => 'instock', 'featured' => true, ); $products = wc_get_products( $args );
in the loop:
$query = new WC_Product_Query( array( 'limit' => 6, 'orderby' => 'title', 'order' => 'ASC', 'category' => $club_cat, 'stock_status' => 'instock', 'featured' => true, 'return' => 'ids', ) ); $products = $query->get_products();
-
- 2020-07-12
人们应该开始使用wc_get_products,因为woocommerce表示这是检索产品的标准方法. wc_get_products具有一个仅需设置为true的参数.所以代码就在下面.
<?php // Display featured products by category. on this case its "shirts" which is the slug of the category. $query_args = array( 'featured' => true, 'category' => array( 'shirts' ), ); $products = wc_get_products( $query_args ); global $post; $columns = wc_get_loop_prop( 'columns' ); ?> <div class="woocommerce columns-<?php echo esc_attr( $columns ); ?>"> <?php woocommerce_product_loop_start(); foreach ($products as $product) { $post = get_post($product->get_id()); setup_postdata($post); wc_get_template_part('content', 'product'); } wp_reset_postdata(); woocommerce_product_loop_end(); ?> </div>
在此处查看完整文章: https://jameshwartlopez.com/plugin/get-featured-products-of-a-category/
People should start using wc_get_products because woocommerce said this is the standard way of retrieving product. wc_get_products has an argument featured which just needs to be set to true. So the code is simply below.
<?php // Display featured products by category. on this case its "shirts" which is the slug of the category. $query_args = array( 'featured' => true, 'category' => array( 'shirts' ), ); $products = wc_get_products( $query_args ); global $post; $columns = wc_get_loop_prop( 'columns' ); ?> <div class="woocommerce columns-<?php echo esc_attr( $columns ); ?>"> <?php woocommerce_product_loop_start(); foreach ($products as $product) { $post = get_post($product->get_id()); setup_postdata($post); wc_get_template_part('content', 'product'); } wp_reset_postdata(); woocommerce_product_loop_end(); ?> </div>
See full post here: https://jameshwartlopez.com/plugin/get-featured-products-of-a-category/
-
- 2016-07-11
如果您查看
wp_postmeta
表中的数据库,将会看到meta_key
将具有_featured
和meta_value
将是yes
或no
,因此不是值0
或1
,而是写yes
或no
<?php $q = new WP_Query([ 'post_type' => 'product', 'stock' => 1, 'showposts' => 3, 'orderby' => 'date', 'order' => 'DESC', 'meta_query' => [ ['key' => '_featured', 'value' => 'yes' ] ] ]); if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post(); // display product info endwhile; wp_reset_query(); endif; ?>
if you take a look in the database in
wp_postmeta
table you will seemeta_key
will be_featured
andmeta_value
will beyes
orno
so instead of value0
or1
writeyes
orno
<?php $q = new WP_Query([ 'post_type' => 'product', 'stock' => 1, 'showposts' => 3, 'orderby' => 'date', 'order' => 'DESC', 'meta_query' => [ ['key' => '_featured', 'value' => 'yes' ] ] ]); if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post(); // display product info endwhile; wp_reset_query(); endif; ?>
-
- 2019-04-03
<ul class="products"> <?php $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(); echo '<p>'.get_the_title().'</p>'; endwhile; } else { echo __( 'No products found' ); } wp_reset_postdata(); ?> </ul><!--/.products-->
<ul class="products"> <?php $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(); echo '<p>'.get_the_title().'</p>'; endwhile; } else { echo __( 'No products found' ); } wp_reset_postdata(); ?> </ul><!--/.products-->
-
请** [编辑]您的答案**,并添加说明:**为什么**可以解决问题?Please **[edit] your answer**, and add an explanation: **why** could that solve the problem?
- 0
- 2019-04-03
- fuxia
我想在home-page.php模板上显示woocommerce商店中的6种特色产品.经过一番研究后,我发现正确的方法是通过自定义循环((我不想使用短代码,因为我想添加其他类来设置样式等.))我还发现woocommerce用于特色产品为" _featured".我将下面的代码放在一起,以显示我选择在商店中成为特色产品的任何产品,但此方法不起作用...不胜感激.