为foreach()提供的参数无效

突然开始为以下代码收到此错误:</ p>

   &lt;?php foreach(get_the_terms(get_the_ID(),'loan-club')为$ cat):?&gt; 
&lt; img src =“&lt;?php echo z_taxonomy_image_url($ cat-&gt; term_id);?&gt; ;” title =“ON LOAN AT:&lt;?php echo $ cat-&gt; name;?&gt;” /&gt;
&lt;?php endforeach; ?&GT;

</ code> </ pre>

有时候,分类学“贷款俱乐部”是空的。 这可能是问题吗? 如果是这样,有人会指出我正确的代码吗?</ p>
</ div>

展开原文

原文

Suddenly starting getting this error for the following code:

<?php foreach (get_the_terms(get_the_ID(), 'loan-club') as $cat) : ?>
<img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" title="ON LOAN AT: <?php echo $cat->name; ?>" />
<?php endforeach; ?> 

Sometimes the taxonomy 'loan-club' is empty. Could that be the problem? If so, could someone point me towards the correct code?

dpdx51205
dpdx51205 “有时分类学'贷款俱乐部'是空的。”这就是问题。如果(is_array(get_the_terms(get_the_ID(),'loan-club')))
3 年多之前 回复
doujuanxun7167
doujuanxun7167 检查此函数的返回类型应该是数组或对象get_the_terms(get_the_ID(),'loan-club')
3 年多之前 回复

2个回答



foreach </ code>之前添加 if </ code>条件:</ p>

 &lt;?php 
$ loan_club = get_the_terms(get_the_ID(),'loan-club');

if(is_array($ loan_club)){
foreach($ loan_club as $ cat) {
?&gt;
&lt; img src =“&lt;?php echo z_taxonomy_image_url($ cat-&gt; term_id);?&gt;” title =“ON LOAN AT:&lt;?php echo $ cat-&gt; name;?&gt;” /&gt;
&lt;?php
}
}
?&gt;
</ code> </ pre>

请查看文档中的get_the_terms()函数: https://developer.wordpress.org/reference/functions/get_the_terms/ </ p>

该函数也可能返回WP_Error或false。 如果返回WP_Error或false,则会导致错误并且foreach循环中断。</ p>
</ div>

展开原文

原文

Add if condition before foreach:

<?php
$loan_club = get_the_terms(get_the_ID(), 'loan-club');

if(is_array($loan_club)) {
    foreach ($loan_club as $cat) {
      ?>
        <img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" title="ON LOAN AT: <?php echo $cat->name; ?>" />
      <?php
    } 
}
?>

Look at get_the_terms() function in the documentation: https://developer.wordpress.org/reference/functions/get_the_terms/

The function may also return WP_Error or false. If WP_Error or false is returned this will cause the error and the foreach loop breaks.

dongquepao8653
dongquepao8653 辉煌! 感谢非常明确和准确的答案。 为什么我以前使用的代码突然填充我的网站有错误?
3 年多之前 回复



</ p>





 &lt;?php foreach((array)get_the_terms(get_the_ID(),'loan-club')as $ cat):?&gt;

&lt; img src =“&lt;?php echo z_taxonomy_image_url($ cat-&gt ; term_id);?&gt;“ title =“ON LOAN AT:&lt;?php echo $ cat-&gt; name;?&gt;” /&gt;

&lt;?php endforeach; ?&gt;

注意:对于所有抱怨类型转换的人,请注意OP如果数组为空则要求最简单的方法跳过foreach(重点是我的)。 值true,false,数字或字符串不被视为空。</ code> </ pre>

</ div>

</ div>

</ div>

展开原文

原文

<?php foreach ((array)get_the_terms(get_the_ID(), 'loan-club') as $cat) : ?>
<img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" title="ON LOAN AT: <?php echo $cat->name; ?>" />
<?php endforeach; ?>

Note: to all the people complaining about typecast, please note that the OP asked cleanest way to skip a foreach if array is empty (emphasis is mine). A value of true, false, numbers or strings is not considered empty.

</div>

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问