从Wordpress中的博客文章的永久链接URL中删除子类别时遇到问题

我正在使用此解决方案从我的博客文章的网址中删除子类别: Remove sub category slug from permalink URL of blog post and custom post type in WordPress

add_filter('post_link','custom_post_type_link',10,3); 

function custom_post_type_link($permalink,$post,$leavename) {
if (!gettype($post) == 'post') {
return $permalink;
}
switch ($post->post_type) {
case 'post':
    //$permalink = get_home_url() . '/' . $post->post_name . '/';

    $cats = get_the_category($post->ID);
    $subcats = array();
    foreach( $cats as $cat ) {
        $cat = get_category($cat->term_id);
        if($cat->parent) { $subcats[] = sanitize_title($cat->name); }
    }
    if($subcats) {
        foreach($subcats as $subcat) {
            $subcat = $subcat.'/';
            $permalink = str_replace($subcat,"",$permalink);
        }
    }


    break;
}

return $permalink;}

它工作正常,但是代码仍然存在一些问题。

如果我有这样的网址:

  

www.myblog.com/parentcategory/ 子类别的名称 / slugpost

我会得到这个:

  

www.myblog.com/parentcategory/slugpost

这正是我想要的。

但是,如果我有这样的网址:

  

www.myblog.com/parentcategory/ 子类别的名称 / slugpost-with- 子类别的名称

我会得到这个:

  

www.myblog.com/parentcategory/slugpost-with- (提示子弹的末尾)

...以及指向本文的每个链接的404页。

所以问题在于,当子类别文本出现在子句的帖子中时,代码还将删除URL的这一部分。

有人知道如何解决这个问题吗?

谢谢!

chenxcl12345 回答:从Wordpress中的博客文章的永久链接URL中删除子类别时遇到问题

尝试划定弹头的两端,例如:

        foreach ($subcats as $subcat) {
            $permalink = str_replace('/'.$subcat.'/','/',$permalink);
        }
,

我找到了这个解决方案,添加了两个功能。我不确定代码是否是最佳代码,但是否能完美工作。

add_filter('post_link','custom_post_type_link',10,3); 

//function to see if URL ends with...
function endsWith($string,$endString) { 
$len = strlen($endString); 
if ($len == 0) { 
    return true; 
} 
return (substr($string,-$len) === $endString); 
} 

//function to replace only first match
function str_replace_first($from,$to,$content)
{
$from = '/'.preg_quote($from,'/').'/';

return preg_replace($from,$content,1);
}

function custom_post_type_link($permalink,$post,$leavename) {
if (!gettype($post) == 'post') {
return $permalink;
}


switch ($post->post_type) {
case 'post':
    //$permalink = get_home_url() . '/' . $post->post_name . '/';


    $cats = get_the_category($post->ID);
    $subcats = array();
    foreach( $cats as $cat ) {
        $cat = get_category($cat->term_id);
        if($cat->parent) { $subcats[] = sanitize_title($cat->name); }
    }

    if($subcats) {
        foreach($subcats as $subcat) {
            $subcat = $subcat.'/';

            //If URL ends with category name
            if(endsWith($permalink,$subcat)){
              //And if the category name appears more than once
              if(substr_count($permalink,$subcat)>1){
                // Remove only first match
                $permalink = str_replace_first($subcat,"",$permalink);
              }else{
                //do nothing
              }
            }
            // If URL does NOT end with category name
            else{
              $permalink = str_replace($subcat,$permalink);
            }
        }
    }
    break;
}

return $permalink;}
本文链接:https://www.f2er.com/3064657.html

大家都在问