当前位置:首页-WordPress文章-WooCommerce-正文

WooCommerce 3 更新后特色产品的获取方法

WooCommerce 已经是 WordPress 平台最好最强大的在线商城插件,已经被 WordPress 收购,更新速度比较频繁,功能更加完善。

在频繁的更新后,一些常用的函数都在优化,一些字段也有所改变。Taji 主题在首页有一个模块『编辑推荐』,获取的是特色产品,在之前我们是可以通过以下代码就可以获取特色产品:

  1. $args = array(
  2.     'post_type' => 'product',
  3.     'posts_per_page'  => 5,
  4.     'meta_key' => '_featured',
  5.     'meta_value' => 'yes'
  6. );

在 WooCommerce 3之后(具体哪个版本不确定),以上代码是获取不了特色产品,百度、谷歌都搜遍了也没有解决,WooCommerce 插件有提供一些简码,其中就有特色产品,所以查看了下简码才得到决定。

所以可以通过以下代码来获取 WooCommerce 的特色产品:

  1. $args = array(
  2.     'post_type' => 'product',
  3.     'posts_per_page' => 5,
  4.     'tax_query' => array(
  5.         array(
  6.             'taxonomy' => 'product_visibility',
  7.             'terms' => 'featured',
  8.             'field' => 'name',
  9.             'operator' => 'IN',
  10.             'include_children' => false
  11.         )
  12.     )
  13. );

以上只是解决方案,获取文件是使用WP_Query函数。

本文原创,作者:萨龙龙,其版权均为萨龙网络所有。
如需转载,请注明出处:https://salongweb.com/woocommerce-3-feature-product.html