我该如何修复此&quot&quot'警告:Explode():空分隔符"带有WordPress类别的错误消息?

发布于 2025-01-24 07:09:19 字数 2404 浏览 1 评论 0原文

我在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.

enter image description here

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.

enter image description here

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深爱不及久伴 2025-01-31 07:09:19

在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;
    }
} 

With the answers given by Martin and aynber, I solved the problem with this code:

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;
    }
} 
执妄 2025-01-31 07:09:19
  $ cats =爆炸($ separator,$ thelist);
 

源值$ separator不允许作为一个空字符串(或equivilant),因此,您可以在运行爆炸 href =“ https://stackoverflow.com/questions/49854998/49854998/ternary-operatorelvis-operator”> ternary oterator 在功能中:

a

确保给定的$ seperator /code>值不是空的;

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"

b

设置您选择的默认分离器值,如果它为空;

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标记不排队,例如&lt; p&gt;在这里。例子。我不能说这是否是您正在发生的事情,但我建议您尝试评论此功能,看看是否可以改善结果。

我建议您可以构建一个更健壮的 preg_replace() 删除特定字符的功能,例如任何不是字母数字等。

$cats = explode($separator,$thelist);

The source value $separatoris not allowed to be an empty string (or equivilant), therefore you can either check the value before you run the explode or you can use a ternary operator within the function:

A

Ensure the given $seperator value is not empty;

if ( !empty($separator) && !empty($thelist) ) {     
    $cats = explode($separator, $thelist);
 }

Example using A here:

$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"

B

Set a default seperator value of your choice if it is empty;

if (!empty($thelist) ) {     
    $cats = explode( ($separator?:' '),$thelist);
 }

Example using B here:

$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.

Further to your question:

$catname = trim(strip_tags($cat));

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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文