2011-09-18 146 views
2

的代码:未定义,偏移量:在0的wordpress

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锉的调试问题:显示Notice: Undefined offset: 0

如何纠正它?谢谢。

回答

3

你有这样的代码在功能:

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

如果文章没有标签,0将是不确定的偏移量$tags。要修复它,请将$tagcount = count($tags);一行向上移动一行,然后只在大于零时构造$taglist。如果它等于零,则可能需要将其设置为空字符串,但您可能需要将其设置为其他值,具体取决于如何使用$taglist

+0

这太复杂了。我可以在$ taglist =“'”之前创建一个条件。 $标签[0] - > term_id。 “'”;像if($ tags) – enjoylife

+0

@enjoylife:我确实提出了一个条件。我只是说你需要在该行之前检查'$ tags'的长度。 – icktoofay

+0

是的我也怀疑你的for循环for'($ i = 1; $ i <$ tagcount; $ i ++){'需要为'($ i = 0; $ i <$ tagcount; $ i ++){' – drew010