Php 警告:sizeof():参数必须是实现可计数的数组或对象
嘿,朋友们,我需要一个解决方案来修复这个错误 警告:sizeof():参数必须是在第18行的C:\xampp\htdocs\my site\wp content\themes\kingdom\woocmerce\content-single-product.php中实现可数的数组或对象 PHP文件的行:Php 警告:sizeof():参数必须是实现可计数的数组或对象,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,嘿,朋友们,我需要一个解决方案来修复这个错误 警告:sizeof():参数必须是在第18行的C:\xampp\htdocs\my site\wp content\themes\kingdom\woocmerce\content-single-product.php中实现可数的数组或对象 PHP文件的行: $cat_count = sizeof( get_the_terms( $post->ID, 'product_cat' ) ); $tag_count = sizeof( get_the
$cat_count = sizeof( get_the_terms( $post->ID, 'product_cat' ) );
$tag_count = sizeof( get_the_terms( $post->ID, 'product_tag' ) );
您应该使用
wp\u count\u terms
,因为您似乎只需要这里的计数
通常
获取\u术语
如果术语存在,则返回任意一个对象;如果不存在,则返回false,这就是出现此错误的原因
因此,只需在代码中添加条件,检查get\u\u terms
是否为真,如果不只是在变量中返回0,则通过添加sizeof
来计算术语:
$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;
什么会让术语()返回?这正是我的意思。但它显然不返回数组或对象。