php - undefined offset : 0 in wordpress

标签 php wordpress wordpress-theming

代码:

function Sail_wp_get_related_posts(){  
    global $wpdb, $post;
    if(!$post->ID){return;}
    $now = current_time('mysql', 1);
    $tags = wp_get_post_tags($post->ID);
    $taglist = "'" . $tags[0]->term_id. "'";
    $tagcount = count($tags);
        $m=1;
    if ($tagcount > 1) {
        for ($i = 1; $i < $tagcount; $i++) {
            $taglist = $taglist . ", '" . $tags[$i]->term_id . "'";
        }
    }

……

我将代码放在functions.php文件中。调试问题:显示注意: undefined offset :0

如何纠正?谢谢。

最佳答案

您在该函数中有以下代码:

$taglist = "'" . $tags[0]->term_id. "'";

如果帖子没有标签,0 将是 $tags 中 undefined offset 。要修复此问题,请将 $tagcount = count($tags); 移至一行,然后仅在它大于零时构造 $taglist。如果它等于零,您可能希望将其设置为空字符串,但您可能希望将其设置为其他内容,具体取决于 $taglist 的使用方式。

关于php - undefined offset : 0 in wordpress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7458674/

相关文章:

wordpress - wp-bakery 的位置和文件夹名称

php - 只是 PHP 无法在 Win7 64 位上安装 Wamp-Server

css - 水平对齐子菜单 Wordpress WPEX Adapt 主题

wordpress - 将Bootstrap轮播与youtube iframe API结合使用时,IE 11出现问题

wordpress - "Remember Me"功能在 WordPress 中无法正常工作

php - 如何在 PHP 中关闭大括号

php - 挣扎于 PHP/MySQL 分页

php - 多选查询数据库

php - 从数组中删除重复值

php - WordPress 如何仅获取自定义帖子类型的父帖子