如何从永久链接中删除一个特定类别(未分类) [英] How to remove one specific category (uncategorized) from post permalinks

查看:145
本文介绍了如何从永久链接中删除一个特定类别(未分类)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在wordpress网站上有几则帖子. 其中大多数属于特定类别,但有些则不属于.

I have several posts on my wordpress site. Most of them belong to a specific category, but some don't.

对于属于某个类别的所有帖子,我希望具有以下永久链接结构: /%category%/%postname%/

For all the posts that belong to a category I want to have the following permalink structure: /%category%/%postname%/

所有其他默认帖子自动"属于未归类类别. 他们的网址结构如下所示:/uncategorized/%postname%/

All the other default posts belong "automatically" to the uncategorized category. Their URL structure will look like this /uncategorized/%postname%/

我想将永久链接更改为以下内容:/%postname%/

I want to change permalinks to the following: /%postname%/

是否可以对属于未分类类别的永久链接进行例外处理?

Is there a way to make an exception for the permalinks that belong to the uncategorized category?

提前谢谢!

推荐答案

解决方案如下为我工作.

在function.php中,只需添加:

In function.php, just add :

if the post is category "uncategorized" or child of "uncategorized" as the main category, change the permalink rule of "/%category%/%postname%" to "/%postname%"

function my_pre_post_link( $permalink, $post, $leavename ) {
  if( $post->post_type != 'post' ) return $permalink;
  $cats = get_the_category($post->ID);
  if( ! count($cats) ) return $permalink;

  usort($cats, '_usort_terms_by_ID');
  $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );

  $category_object = get_term( $category_object, 'category' );

  return _clear_uncategorized($category_object, $permalink);
}

function _clear_uncategorized($cat, $permalink) {
  if( $cat->slug == 'uncategorized' ) {
    return str_replace('%category%/', '', $permalink);
  }
  $parent = $cat->parent;
  if ( !$parent )
    return $permalink;
  return _clear_uncategorized($parent, $permalink);
}

add_filter( 'pre_post_link', 'my_pre_post_link', 9, 3 );

这篇关于如何从永久链接中删除一个特定类别(未分类)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆