代码:
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/