2009-09-03 140 views
3

我试图定义WordPress的一个新的短代码,和我当功能被加载以下错误(只装,我还没有试过打电话给它的任何地方尚未):count()导致“意外的T_STRING”错误?

Parse error: syntax error, unexpected T_STRING in /pathtomytheme/user_functions.php on line 105 

这里的码;线105为 “$ cat_n =计数($猫) - 1;”:

function usertag_2colcats($atts) { 
extract(shortcode_atts(array('parent' => 0,'depth' => 2,), $atts)); 
$cats = explode('<br />', wp_list_categories('title_li=&echo=0&depth=' . $depth . '&style=none&show_count=1&use_desc_for_title=0&child_of=' . $parent)); 
$cat_n = count($cats) – 1; 
for ($i = 0; $i < $cat_n; $i++) { 
    if ($i < $cat_n/2) $cat_left = $cat_left . '<li>' . $cats[$i] . '</li>'; 
    elseif ($i >= $cat_n/2) $cat_right = $cat_right.'<li>'.$cats[$i].'</li>'; 
} 
echo '<ul class="leftcats">' . $cat_left . '</ul><ul class="rightcats">' . $cat_right . '</ul>'; 

}

如果我改变该行,使得它不使用计数功能,例如到“$ cat_n = 5;”,该函数加载没有错误。似乎我错过了一些非常明显的东西;它是什么?

原始代码是在这里:http://pcsplace.com/blog-tips/how-to-split-categories-list-into-columns-in-wordpress/

回答

4

这听起来不可思议,但“ - ”号在线路105是一个奇怪的字符。尝试重写那一行,而不是复制并粘贴它。我做到了,错误消失了。

编辑:好吧,这就是我发现的。第105行中的字符具有ASCII代码226.但是,减号的ASCII代码是45.因此,您的问题肯定存在。

不惜一切代价避免复制粘贴;)

+0

+1好点。 PHP将ASCII 226视为一个'单词'字符,因此它不再有效'$ cat_n = count($ cats)hello 1;' – 2009-09-03 23:49:37

+0

谢谢rogeriopvl,你是对的。现在一切顺利。 – Becca 2009-09-04 02:51:42

1

你有没有试过var_dump($cats)

有时候COUNT()可以根据某些情况下返回false,但在这种情况下,我敢肯定它只有返回整个字符串,因为它并没有发现它。

相关问题