【问题标题】:Warning: sizeof(): Parameter must be an array or an object that implements Countable警告:sizeof():参数必须是数组或实现 Countable 的对象
【发布时间】:2019-01-27 13:26:47
【问题描述】:

嘿朋友我需要一个解决方案来解决这个错误

警告:sizeof(): 参数必须是数组或对象,在 C:\xampp\htdocs\my-site\wp-content\themes\kingdom\woocommerce\content-single-product.php 中实现 Countable第 18 行

PHP 文件行:

$cat_count = sizeof( get_the_terms( $post->ID, 'product_cat' ) );
$tag_count = sizeof( get_the_terms( $post->ID, 'product_tag' ) ); 

【问题讨论】:

  • get_the_terms() 返回什么?
  • 正是我的意思。但它显然没有返回数组或对象。

标签: php wordpress woocommerce


【解决方案1】:

通常get_the_terms 如果术语存在则返回任一对象,否则返回 false,这就是您出现此错误的原因。

所以只需在您的代码中添加条件以检查 get_the_terms 是否为真,通过添加 sizeof 来计算条款,如果不只是在您的变量中返回 0:

$cat_count = (get_the_terms($post->ID, 'product_cat')) ? sizeof(get_the_terms($post->ID, 'product_cat')) : 0;
$tag_count = (get_the_terms($post->ID, 'product_tag')) ? sizeof(get_the_terms($post->ID, 'product_tag')) : 0;

Code Reference

【讨论】:

    【解决方案2】:

    您应该使用wp_count_terms,因为您似乎只需要这里的计数

    https://developer.wordpress.org/reference/functions/wp_count_terms/

    【讨论】:

      猜你喜欢
      • 2020-05-28
      • 2018-12-19
      • 1970-01-01
      • 2018-09-05
      • 2019-08-15
      • 2020-01-18
      • 2018-09-10
      • 2020-03-23
      • 2020-01-27
      相关资源
      最近更新 更多