如何使用 WordPress 类别修复此 "Warning: explode(): Empty delimiter" 错误消息?
How do I fix this "Warning: explode(): Empty delimiter" error message with WordPress categories?
我正在使用的 WordPress 主题 functions.php 中有以下功能。它应该排除指定的 post 类别(例如“未分类”)显示在网站的前端。它确实适用于此目的。
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);
但是,在某些自定义 post 类型中,其类别与我在上述代码中选择的类别不同,当我创建新的 post 时,出现此错误
Warning: explode(): Empty delimiter
在 post 编辑器右侧的类别选择面板中。
这一行是它说的不正确的:
$cats = explode($separator,$thelist);
我试过将那部分代码包装在 if
条件中以检查空分隔符,如下所示:
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);
}
但是虽然这确实使错误从“类别”面板中消失了,但结果是在类别名称应该出现的地方出现了一堆空白。
我做错了什么?
$cats = explode($separator,$thelist);
源值 $separator
不允许为空字符串(或等效字符串),因此您可以在 运行 和 explode
之前检查该值,或者您可以使用 函数内:
一个
确保给定的 $seperator
值不为空;
if ( !empty($separator) && !empty($thelist) ) {
$cats = explode($separator, $thelist);
}
此处使用 A 的示例:
$seperator = "";
$thelist = "1 2 3 4 5 6 7 8 9";
$cats = "failed";
if ( !empty($separator) && !empty($thelist) ) {
$cats = explode($separator, $thelist);
}
print_r($cats);
// output string "failed"
乙
如果为空,则设置您选择的默认分隔符值;
if (!empty($thelist) ) {
$cats = explode( ($separator?:' '),$thelist);
}
此处使用 B 的示例:
$seperator = "";
$thelist = "1 2 3 4 5 6 7 8 9";
$cats = "failed";
if (!empty($thelist) ) {
$cats = explode(($separator?:' '),$thelist);
}
print_r($cats);
// output array of numerical values.
关于你的问题:
$catname = trim(strip_tags($cat));
“Strip_tags”函数问题极大,如果字符串中的HTML标签没有对齐,很容易return空字符串,例如<p>something here.
如您所举,这可能会导致空字符串。我不能说这是否是你正在发生的事情,但我建议你尝试注释掉这个函数,看看是否能改善结果。
我建议您可以为自己构建一个更强大的 preg_replace()
功能来删除特定字符,例如任何非字母数字的字符等。
根据Martin和aynber给出的答案,我用这段代码解决了问题:
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
if ( !empty( $separator ) && !empty( $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);
} else {
return $thelist;
}
}
我正在使用的 WordPress 主题 functions.php 中有以下功能。它应该排除指定的 post 类别(例如“未分类”)显示在网站的前端。它确实适用于此目的。
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);
但是,在某些自定义 post 类型中,其类别与我在上述代码中选择的类别不同,当我创建新的 post 时,出现此错误
Warning: explode(): Empty delimiter
在 post 编辑器右侧的类别选择面板中。
这一行是它说的不正确的:
$cats = explode($separator,$thelist);
我试过将那部分代码包装在 if
条件中以检查空分隔符,如下所示:
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);
}
但是虽然这确实使错误从“类别”面板中消失了,但结果是在类别名称应该出现的地方出现了一堆空白。
我做错了什么?
$cats = explode($separator,$thelist);
源值 $separator
不允许为空字符串(或等效字符串),因此您可以在 运行 和 explode
之前检查该值,或者您可以使用
一个
确保给定的 $seperator
值不为空;
if ( !empty($separator) && !empty($thelist) ) {
$cats = explode($separator, $thelist);
}
此处使用 A 的示例:
$seperator = "";
$thelist = "1 2 3 4 5 6 7 8 9";
$cats = "failed";
if ( !empty($separator) && !empty($thelist) ) {
$cats = explode($separator, $thelist);
}
print_r($cats);
// output string "failed"
乙
如果为空,则设置您选择的默认分隔符值;
if (!empty($thelist) ) {
$cats = explode( ($separator?:' '),$thelist);
}
此处使用 B 的示例:
$seperator = "";
$thelist = "1 2 3 4 5 6 7 8 9";
$cats = "failed";
if (!empty($thelist) ) {
$cats = explode(($separator?:' '),$thelist);
}
print_r($cats);
// output array of numerical values.
关于你的问题:
$catname = trim(strip_tags($cat));
“Strip_tags”函数问题极大,如果字符串中的HTML标签没有对齐,很容易return空字符串,例如<p>something here.
如您所举,这可能会导致空字符串。我不能说这是否是你正在发生的事情,但我建议你尝试注释掉这个函数,看看是否能改善结果。
我建议您可以为自己构建一个更强大的 preg_replace()
功能来删除特定字符,例如任何非字母数字的字符等。
根据Martin和aynber给出的答案,我用这段代码解决了问题:
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
if ( !empty( $separator ) && !empty( $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);
} else {
return $thelist;
}
}