php 警告:implode() [function.implode]:传递的参数无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5280180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning: implode() [function.implode]: Invalid arguments passed
提问by Scott B
I'm getting the error below...
我收到以下错误...
Warning: implode() [function.implode]: Invalid arguments passed in \wp-content/themes/mytheme/functions.php on line 1335
警告:implode() [function.implode]:在第 1335 行的 \wp-content/themes/mytheme/functions.php 中传递的参数无效
at...
在...
function my_get_tags_sitemap(){
if ( !function_exists('wp_tag_cloud') || get_option('cb2_noposttags')) return;
$unlinkTags = get_option('cb2_unlinkTags');
echo '<div class="tags"><h2>Tags</h2>';
if($unlinkTags)
{
$tags = get_tags();
foreach ($tags as $tag){
$ret[]= $tag->name;
}
//ERROR OCCURS HERE
echo implode(', ', $ret);
}
else
{
wp_tag_cloud('separator=, &smallest=11&largest=11');
}
echo '</div>';
}
Any ideas how to intercept the error. The site has exactly one tag.
任何想法如何拦截错误。该网站只有一个标签。
回答by Mark Eirich
You are getting the error because $ret
is not an array.
您收到错误,因为$ret
它不是数组。
To get rid of the error, at the start of your function, define it with this line: $ret = array();
要消除错误,请在函数的开头使用以下行定义它: $ret = array();
It appears that the get_tags() call is returning nothing, so the foreach is not run, which means that $ret isn't defined.
似乎 get_tags() 调用没有返回任何内容,因此没有运行 foreach,这意味着未定义 $ret。
回答by u1362399
You can try
你可以试试
echo implode(', ', (array)$ret);
回答by Andrew Moore
It happens when $ret
hasn't been defined. The solution is simple. Right above $tags = get_tags();
, add the following line:
它发生在$ret
尚未定义的时候。解决方法很简单。在正上方$tags = get_tags();
,添加以下行:
$ret = array();
回答by hasnath rumman
function my_get_tags_sitemap(){
if ( !function_exists('wp_tag_cloud') || get_option('cb2_noposttags')) return;
$unlinkTags = get_option('cb2_unlinkTags');
echo '<div class="tags"><h2>Tags</h2>';
$ret = []; // here you need to add array which you call inside implode function
if($unlinkTags)
{
$tags = get_tags();
foreach ($tags as $tag){
$ret[]= $tag->name;
}
//ERROR OCCURS HERE
echo implode(', ', $ret);
}
else
{
wp_tag_cloud('separator=, &smallest=11&largest=11');
}
echo '</div>';
}