
[英]Fatal error: Uncaught TypeError: count(): Argument #1 ($var) must be of type Countable|array, null given in
[英]PHP Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, string given in
对于以下代码行,在 PHP 8 中触发了上述警告:
if ( isset( $attachment_metadata['sizes'] ) && count( $attachment_metadata['sizes'] ) && ( isset( $attachment_metadata['sizes'][ $intermediate_size ] ) ) ) {
整个 function 是:
function wpsc_product_image( $attachment_id = 0, $width = null, $height = null ) {
// Do some dancing around the image size
if ( ( ( $width >= 10 ) && ( $height >= 10 ) ) && ( ( $width <= 1024 ) && ( $height <= 1024 ) ) ) {
$intermediate_size = "wpsc-{$width}x{$height}";
}
// Get image url if we have enough info
if ( $attachment_id > 0 && ! empty( $intermediate_size ) ) {
// Get all the required information about the attachment
$uploads = wp_upload_dir();
$image_meta = get_post_meta( $attachment_id, '' );
$file_path = get_attached_file( $attachment_id );
// Clean up the meta array
foreach ( $image_meta as $meta_name => $meta_value ) {
$image_meta[ $meta_name ] = maybe_unserialize( array_pop( $meta_value ) );
}
$attachment_metadata = isset( $image_meta['_wp_attachment_metadata'] ) ? $image_meta['_wp_attachment_metadata'] : null;
// Determine if we already have an image of this size
if ( isset( $attachment_metadata['sizes'] ) && count( $attachment_metadata['sizes'] ) && ( isset( $attachment_metadata['sizes'][ $intermediate_size ] ) ) ) {
$intermediate_image_data = image_get_intermediate_size( $attachment_id, $intermediate_size );
$image_url = $intermediate_image_data['url'];
} else {
$image_url = home_url( "index.php?wpsc_action=scale_image&attachment_id={$attachment_id}&width=$width&height=$height" );
}
// Not enough info so attempt to fallback
} else {
if ( ! empty( $attachment_id ) ) {
$image_url = home_url( "index.php?wpsc_action=scale_image&attachment_id={$attachment_id}&width=$width&height=$height" );
} else {
$image_url = false;
}
}
if ( empty( $image_url ) && ! empty( $file_path ) ) {
$image_meta = get_post_meta( $attachment_id, '_wp_attached_file' );
if ( ! empty( $image_meta ) ) {
$image_url = $uploads['baseurl'] . '/' . $image_meta[0];
}
}
在尝试解决这个问题时遇到困难。
您传递给count()
的参数$attachment_metadata['sizes']
是一个字符串,但count()
需要不同数据类型的对象: array
或实现Countable接口的 class。 尝试检查$attachment_metadata['sizes']
中的值以确认它是否符合您的预期。 根据消息,它与count()
不兼容。 也许您希望strlen()
检查存储在$attachment_metadata['sizes']
中的值是否为空。
如果您不确定,可以在调用count()
之前使用get_class()
、 gettype()
或class_implements()
(对于接口)验证数据类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.