WordPress 页面(php 文件)上出现错误:参数必须是实现 Countable 的数组或对象
我是新手,不懂php。我在 WordPress 帖子中收到此错误:
警告:count():参数必须是在 /www/tastingvictoria_289/public/wp-content/themes/astra-child/single.php 中实现 Countable 的数组或对象第 32 行 其中 if(count($lated_article) > 0){?>
是。
这是它来自的文件。这是语法问题吗?
有什么提示吗?谢谢
<div id="primary" <?php astra_primary_class(); ?>>
<?php astra_primary_content_top(); ?>
<?php
$related_article = get_field( "select_article", 'option' );
if(count($related_article) > 0){
?>
<?php
?>
<div class="article">
<div class="wrap">
<a href="<?php echo $link; ?>">
<div class="article-image" style="background-image: url(<?php echo $post_thumb; ?>);"></div>
</a>
<div class="infos">
<div class="infos-wrap">
<p class="date"><?php echo $postdate; ?></p>
<a href="<?php echo $link; ?>"><h4 class="title"><?php echo $title; ?></h4></a>
<p class="excerpt"><?php echo $excerpt; ?></p>
</div>
</div>
</div>
</div>
<?php
}
?>
<?php
?>
</div><!-- #primary -->
<?php if ( astra_page_layout() == 'right-sidebar' ) : ?>
<?php get_sidebar(); ?>
<?php endif ?>
I am new to this and I don't know php. I get this error on WordPress posts:
Warning: count(): Parameter must be an array or an object that implements Countable in /www/tastingvictoria_289/public/wp-content/themes/astra-child/single.php on line 32
Which where if(count($related_article) > 0){?>
is.
Here is the file it is coming from. Is it a syntax thing?
Any cue? Thanks
<div id="primary" <?php astra_primary_class(); ?>>
<?php astra_primary_content_top(); ?>
<?php
$related_article = get_field( "select_article", 'option' );
if(count($related_article) > 0){
?>
<?php
?>
<div class="article">
<div class="wrap">
<a href="<?php echo $link; ?>">
<div class="article-image" style="background-image: url(<?php echo $post_thumb; ?>);"></div>
</a>
<div class="infos">
<div class="infos-wrap">
<p class="date"><?php echo $postdate; ?></p>
<a href="<?php echo $link; ?>"><h4 class="title"><?php echo $title; ?></h4></a>
<p class="excerpt"><?php echo $excerpt; ?></p>
</div>
</div>
</div>
</div>
<?php
}
?>
<?php
?>
</div><!-- #primary -->
<?php if ( astra_page_layout() == 'right-sidebar' ) : ?>
<?php get_sidebar(); ?>
<?php endif ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
说明:
count() 方法与数组一起使用时,对数组中的所有元素进行计数。
条件:
count($lated_article) > 0
检查数组是否不为空。因此变量
$lated_article
必须是一个数组。php 识别出这不是一个数组并抛出一个错误。
这不是语法错误,而是对 count() 方法的误用。
php 文档 - https://www.php.net/manual/en/function .count.php
解决方案
要修复此错误,我会尝试更改代码
count($lated_article) > 0
到那个
isset($lated_article)
Explanation:
The count() Method Counts all elements in an array when used with an array.
the condition:
count($related_article) > 0
checks if the array is not null.therefore the variable
$related_article
must be an array.php recognizes thats not an array and throws an error.
its not a syntax error but a missuse of the count() method.
php documentaion - https://www.php.net/manual/en/function.count.php
solution
to fix this error i would try to change code
count($related_article) > 0
to that
isset($related_article)