简体   繁体   English

我正在开发一个 wordpress 主题并且有一个警告。 如何解决这个问题

[英]I am developing a wordpress theme and there is a warning. How to resolve this

Warning is " Invalid argument supplied for foreach() "警告是“为 foreach() 提供的参数无效

<?php
    $tags = get_the_tags();
    foreach($tags as $tag):?>

        <a href="<?php echo get_tag_link($tag->term_id);?>" class="badge badge-success">
            <?php echo $tag->name;?>
        </a>

    <?php endforeach;?>

Check if your $tags variable is an actual array of elements before trying to loop through it all...在尝试遍历所有元素之前检查您的 $tags 变量是否是实际的元素数组...

ie: IE:

<?php
$tags = get_the_tags(array('hide_empty' => false)); // show all tags, even empty 1s

// debug - uncomment below to view output
/* 
   echo "<pre>";
   print_r($tags);
   echo "</pre>";
*/

if( is_array($tags) )
{
   foreach($tags as $tag):
    ?>
      <a href="<?php echo get_tag_link($tag->term_id);?>" class="badge badge-success">
         <?php echo $tag->name;?>
      </a>
   <?php 
   endforeach; 
}else{
  echo "<p>Not an Array!</p>";
}
?>

In general, don't assume that a variable returned from a function will always be an array, you can check this before calling foreach like this:通常,不要假设从 function 返回的变量总是一个数组,您可以在调用foreach之前检查这一点,如下所示:

$tags = get_the_tags();
if (is_array($tags) || is_object($tags)) {
    foreach ($tags as $tag) {
        ...
    }
}

In the specific case of get_the_tags() , this might be enough though:get_the_tags()的特定情况下,这可能就足够了:

$tags = get_the_tags();
if ($tags) {
    foreach ($tags as $tag) {
        ...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在实时站点上开发 Wordpress 主题 - Developing Wordpress theme on a live site 如何修复 WordPress 插件对表单处理程序文件的 ajax 请求。 (我自己正在开发一个插件) - How to fix ajax request to the form handler file from the WordPress plugin. (I am developing a plugin myself) 开发Wordpress主题时出现奇怪的错误 - Strange error when developing a Wordpress theme 具有分页的Php搜索代码(使用php)由于警告而不生成导航链接。 我该如何纠正错误? - Php search code with pagination(Using php) is not producing the navigation links because of a warning. How do i correct the error? 我没有在主题中包含任何文件,然后WordPress功能如何工作 - I am not including any file in my theme then how wordpress functions are working 我在自定义wordpress主题中做错了什么 - What am I doing wrong in custom wordpress theme Wordpress 自定义主题 - 我无法使用此代码显示徽标 - Wordpress custom theme - I am unable to display the logowith this code 如何将WordPress主题分为两列? - How do I split a WordPress theme into 2 columns? Wordpress 主题:警告:非法字符串偏移“侧边栏” - Wordpress Theme: Warning: Illegal string offset 'sidebar' ftp_put()函数出现问题。 得到警告。 如何解决这个问题? - Issue with ftp_put() function. getting warning. How to fix this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM