警告:sizeof():参数必须是实现Countable的数组或对象

Vik*_*tor 2 php wordpress woocommerce

嘿朋友,我需要解决此错误的解决方案

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

PHP文件行:

$cat_count = sizeof( get_the_terms( $post->ID, 'product_cat' ) );
$tag_count = sizeof( get_the_terms( $post->ID, 'product_tag' ) ); 
Run Code Online (Sandbox Code Playgroud)

kas*_*alo 7

通常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;
Run Code Online (Sandbox Code Playgroud)

代码参考