从Wordpress中博客文章的永久链接URL中删除子类别时出现问题
我正在使用此解决方案从我的博客帖子的url中删除子类别: 它工作正常,但代码仍然有一个小问题 如果我有这样一个URL: www.myblog.com/parentcategory/儿童类别名称/slugpost 我会得到这个: www.myblog.com/parentcategory/slugpost 这正是我想要的 但如果我有这样一个URL: www.myblog.com/parentcategory/nameofchildcategory/slugpost with-nameofchildcategory 我会得到这个: www.myblog.com/parentcategory/slugpost-with-(mssing the end of the slug) …和一个404页的链接到这篇文章 所以问题是,当slug的帖子中出现子类别文本时,代码也会删除URL的这一部分 有人知道如何解决这个问题吗从Wordpress中博客文章的永久链接URL中删除子类别时出现问题,wordpress,url,categories,blogs,slug,Wordpress,Url,Categories,Blogs,Slug,我正在使用此解决方案从我的博客帖子的url中删除子类别: 它工作正常,但代码仍然有一个小问题 如果我有这样一个URL: www.myblog.com/parentcategory/儿童类别名称/slugpost 我会得到这个: www.myblog.com/parentcategory/slugpost 这正是我想要的 但如果我有这样一个URL: www.myblog.com/parentcategory/nameofchildcategory/slugpost with-nameofchil
提前谢谢 尝试划分段塞的两端,如:
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, $to, $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;}
用此代码替换也会删除父类别“blogue”
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, $to, $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;}