php - 警告:sizeof():参数必须是数组或实现 Countable 的对象
问题描述
嘿朋友我需要一个解决方案来解决这个错误
警告: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
返回任一对象,否则返回 false,这就是您出现此错误的原因。
因此,只需在您的代码中添加条件以检查是否get_the_terms
为真,通过 sizeof
在变量中添加 if not just return 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;
推荐阅读
- jenkins - Jenkins 运行旧代码,而工作区显示最新代码
- whatsapp - 从whatsapp向客户发送消息
- sql - 在 Oracle apex 20.1.0.00.13 中显示来自远程服务器的文件(日志文件)的内容
- loops - 使用两个 foreach 循环
- java - 如何在 JPA Query 中将 LocalDateTime 转换为 LocalDate?
- python-3.x - Python - 如何获取表ID
- javascript - 如何使用角度 6、7、9 中的按钮值动态加载 API
- docker - 如何在 Podman 容器中运行重启命令?
- html - 如何制作一个重定向到 Google 协作平台上另一个页面的 HTML 登录页面?
- python - 是否有 Python 函数或方法可以对列表执行交叉验证?