【问题标题】:Displays the total number of products显示产品总数
【发布时间】:2016-11-25 09:13:45
【问题描述】:

什么代码用于显示产品总数?我想在我的商店主页上创建一个带有图标的部分,包含类似的内容会很棒。

最好的问候

【问题讨论】:

  • 商店或特定类别的产品总数?到目前为止你尝试了什么代码,在那里显示?
  • 无。因为我找不到任何解决方案:/。产品总数,但两种情况都可能有用。
  • 查看答案,你可以得到你店里的产品总数

标签: php wordpress html woocommerce


【解决方案1】:

如果您想显示商店中已发布产品的总数,请使用以下代码:

$args = array( 'post_type' => 'product', 'post_status' => 'publish', 
'posts_per_page' => -1 );
$products = new WP_Query( $args );
echo $products->found_posts;

它返回您商店中已发布产品的总数。

【讨论】:

  • 一个班轮$total_products = (new WP_Query(['post_type' => 'product', 'post_status' => 'publish']))->found_posts;
  • 还有更通用的函数方法$total = fn($post_type, $post_status) => (new WP_Query(['post_type' => $post_type, 'post_status' => $post_status]))->found_posts; 这样调用:$total('product', 'publish')
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-12
  • 2013-11-01
  • 1970-01-01
  • 2021-03-26
  • 1970-01-01
  • 2015-10-08
相关资源
最近更新 更多