我该如何修复此&quot&quot'警告:Explode():空分隔符"带有WordPress类别的错误消息?
我在wordpress主题的function.php中具有以下功能。它应该将指定的帖子类别(例如“未分类”)排除在网站前端显示。它确实为此目的有效。
function the_category_filter($thelist,$separator=' ') {
// list the IDs of the categories to exclude
$exclude = array(1);
// create an empty array
$exclude2 = array();
// loop through the excluded IDs and get their actual names
foreach($exclude as $c) {
// store the names in the second array
$exclude2[] = get_cat_name($c);
}
// get the list of categories for the current post
$cats = explode($separator,$thelist);
// create another empty array
$newlist = array();
foreach($cats as $cat) {
// remove the tags from each category
$catname = trim(strip_tags($cat));
// check against the excluded categories
if(!in_array($catname,$exclude2))
// if not in that list, add to the new array
$newlist[] = $cat;
}
// return the new, shortened list
return implode($separator,$newlist);
}
// add the filter to 'the_category' tag
add_filter('the_category','the_category_filter', 10, 2);
但是,在某些具有不同类别的自定义帖子类型中,与我在上述代码中选择的类别相比,当我创建新帖子时,我会收到此错误
警告:Explode():空分隔符
邮政编辑器右侧的类别选择面板中的空分隔符。
这一行是说不正确的一行:
$ cats = esge($ separator,$ thelist);
我尝试将代码的该部分包装在<<代码>如果可以检查空的定界符的条件,则是这样的:
if ( !empty( explode($separator,$thelist) ) ) {
$cats = explode($separator,$thelist);
// create another empty array
$newlist = array();
foreach($cats as $cat) {
// remove the tags from each category
$catname = trim(strip_tags($cat));
// check against the excluded categories
if(!in_array($catname,$exclude2))
// if not in that list, add to the new array
$newlist[] = $cat;
}
// return the new, shortened list
return implode($separator,$newlist);
}
但是,虽然确实会使错误从类别面板中消失,但结果是一堆应该出现的空白。
我在做什么错?
I have the below function in functions.php of the WordPress theme I'm working with. It's supposed to exclude a designated post category (such as "Uncategorized") from being displayed on the front-end of a website. It does work for that purpose.
function the_category_filter($thelist,$separator=' ') {
// list the IDs of the categories to exclude
$exclude = array(1);
// create an empty array
$exclude2 = array();
// loop through the excluded IDs and get their actual names
foreach($exclude as $c) {
// store the names in the second array
$exclude2[] = get_cat_name($c);
}
// get the list of categories for the current post
$cats = explode($separator,$thelist);
// create another empty array
$newlist = array();
foreach($cats as $cat) {
// remove the tags from each category
$catname = trim(strip_tags($cat));
// check against the excluded categories
if(!in_array($catname,$exclude2))
// if not in that list, add to the new array
$newlist[] = $cat;
}
// return the new, shortened list
return implode($separator,$newlist);
}
// add the filter to 'the_category' tag
add_filter('the_category','the_category_filter', 10, 2);
However, in certain custom post types with different categories than the one that I'm selecting for in the above code, when I'm creating a new post, I get this error
Warning: explode(): Empty delimiter
in the Categories selection panel on the right-hand side of the post editor.
This line is the one it's saying is incorrect:
$cats = explode($separator,$thelist);
I've tried wrapping that part of the code in an if
condition to check for the empty delimiter, like this:
if ( !empty( explode($separator,$thelist) ) ) {
$cats = explode($separator,$thelist);
// create another empty array
$newlist = array();
foreach($cats as $cat) {
// remove the tags from each category
$catname = trim(strip_tags($cat));
// check against the excluded categories
if(!in_array($catname,$exclude2))
// if not in that list, add to the new array
$newlist[] = $cat;
}
// return the new, shortened list
return implode($separator,$newlist);
}
But while that does make the error disappear from the Categories panel, what results is a bunch of blanks where the category names ought to appear.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在Martin和Aynber给出的答案中,我解决了此代码的问题:
With the answers given by Martin and aynber, I solved the problem with this code:
源值
$ separator
不允许作为一个空字符串(或equivilant),因此,您可以在运行爆炸
href =“ https://stackoverflow.com/questions/49854998/49854998/ternary-operatorelvis-operator”> ternary oterator 在功能中:a
确保给定的
$ seperator /code>值不是空的;
示例在此处使用 a :
b
设置您选择的默认分离器值,如果它为空;
在此处使用 b 的示例:
进一步到您的问题:
“ strip_tags”函数非常有问题,如果字符串中的html标记不排队,例如
&lt; p&gt;在这里。例子。我不能说这是否是您正在发生的事情,但我建议您尝试评论此功能,看看是否可以改善结果。
我建议您可以构建一个更健壮的
preg_replace()
删除特定字符的功能,例如任何不是字母数字等。The source value
$separator
is not allowed to be an empty string (or equivilant), therefore you can either check the value before you run theexplode
or you can use a ternary operator within the function:A
Ensure the given
$seperator
value is not empty;Example using A here:
B
Set a default seperator value of your choice if it is empty;
Example using B here:
Further to your question:
The "Strip_tags" function is extremely problematic and can easily return empty strings if the HTML tags in the string do not line up, such as
<p>something here.
this can result in empty strings as you example. I can't say if this is what's happening for you but I would suggest you try and comment out this function and see if that improves the result.I would suggest you can build yourself a more robust
preg_replace()
functionality to remove specific characters such as anything that is not alphanumeric, etc..