PHP: count(): Parameter必须是一个数组或实现Countable的对象



我是php新手。我在一些Wordpress页面上出现了这些错误。

警告:count(): Parameter必须是在/www/tastingvictoria_289/public/wp-content/themes/astra-child/template-parts/content-single.php第38行实现Countable的数组或对象

警告:为/www/tastingvictoria_289/public/wp-content/themes/astra-child/template-parts/content-single.php中的foreach()提供的参数无效

这是相关代码

<?php $terms = get_the_terms( $post->ID , 'category' ); 
$total = count($terms); // 38
$i=0;
foreach ( $terms as $term ) {
if($term->slug != "featured-post"){
$i++;
$term_link = get_term_link( $term, 'category' );
if( is_wp_error( $term_link ) )
continue;
echo '<p class="category"><span><a class="" href="' . $term_link . '">' . $term->name . '</a></span></p>';
if ($i != $total) echo ' ';
}

} 
?>

任何解释吗?

作为错误消息,$term参数count()函数不可数(在某些情况下-例如,post id不是exit)。

要解决这个问题,请将代码更改为:

<?php $terms = get_the_terms( $post->ID , 'category' );
if(is_array($terms)){
$total = count($terms); // 38
$i=0;
foreach ( $terms as $term ) {
if($term->slug != "featured-post"){
$i++;
$term_link = get_term_link( $term, 'category' );
if( is_wp_error( $term_link ) )
continue;
echo '<p class="category"><span><a class="" href="' . $term_link . '">' . $term->name . '</a></span></p>';
if ($i != $total) echo ' ';
}

}
}
?>

$terms转换为数组,以获得count($terms)的有效结果

get_the_terms()return如果成功,则返回WP_Term对象的数组,如果没有条件或帖子不存在则返回false,如果失败则返回WP_Error。

可能是条款不存在。试一试:

$terms = get_the_terms( $post->ID , 'category' );
if ( $terms && ! is_wp_error( $terms ) ) : 
$total = count($terms);
$i=0;
foreach ( $terms as $term ) {
if($term->slug != "featured-post"){
$i++;
$term_link = get_term_link( $term, 'category' );
if( is_wp_error( $term_link ) )
continue;
echo '<p class="category"><span><a class="" href="' . $term_link . '">' . $term->name . '</a></span></p>';
if ($i != $total) echo ' ';
}

}
endif;

主要是下面代码背后的get_the_terms()函数

function get_the_terms( $post, $taxonomy ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}

$terms = get_object_term_cache( $post->ID, $taxonomy );
if ( false === $terms ) {
$terms = wp_get_object_terms( $post->ID, $taxonomy );
if ( ! is_wp_error( $terms ) ) {
$term_ids = wp_list_pluck( $terms, 'term_id' );
wp_cache_add( $post->ID, $term_ids, $taxonomy . '_relationships' );
}
}

/**
* Filters the list of terms attached to the given post.
*
* @since 3.1.0
*
* @param WP_Term[]|WP_Error $terms    Array of attached terms, or WP_Error on failure.
* @param int                $post_id  Post ID.
* @param string             $taxonomy Name of the taxonomy.
*/
$terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy );

if ( empty( $terms ) ) {
return false;
}

return $terms;
}

在这种情况下,你可以看到,如果没有post,它将返回false,如果函数返回false,那么你的代码将是这样的:

<?php $terms = get_the_terms( $post->ID , 'category' ); 
$total = count($terms = false); // if there is no post
$i=0;
foreach ( false as $term ) { //if there is no post
if($term->slug != "featured-post"){
$i++;
$term_link = get_term_link( $term, 'category' );
if( is_wp_error( $term_link ) )
continue;
echo '<p class="category"><span><a class="" href="' . $term_link . '">' . $term->name . '</a></span></p>';
if ($i != $total) echo ' ';
}

} 
?>

注意:count()函数接受数组或对象,foreach()函数也接受iterable_expression。这就是为什么你会收到警告。

因此,在这种情况下,您可以像这样检查get_the_terms()函数的返回输出:
<?php $terms = get_the_terms( $post->ID , 'category' );
if(is_iterable($terms)){
$total = count($terms); // 38
$i=0;
foreach ( $terms as $term ) {
if($term->slug != "featured-post"){
$i++;
$term_link = get_term_link( $term, 'category' );
if( is_wp_error( $term_link ) )
continue;
echo '<p class="category"><span><a class="" href="' . $term_link . '">' . $term->name . '</a></span></p>';
if ($i != $total) echo ' ';
}

}
}else{
//do something
}
?>

谢谢

相关内容

  • 没有找到相关文章

最新更新