1

WordPress如何删除文章链接中的父级分类名

 2 years ago
source link: https://www.huhexian.com/16616.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

WordPress如何删除文章链接中的父级分类名

2021-12-1311:48:34评论573字

我们在用WordPress建立网站并将固定链接使用/%category%/的时候,如果我们的子分类较多,这样就会导致我们的url链接长度过长,看起来很不舒服,那么如果想要从WordPress的链接结构中删除父类别,我们应该如何操作呢?下面就来给大家介绍一下。

WordPress如何删除文章链接中的父级分类名

只需将以下代码粘贴到你正在使用的WordPress主题的functions.php文件中:

  1. add_filter( 'post_link', 'dahuzi_remove_parent_cats_from_link', 10, 3 );
  2. function dahuzi_remove_parent_cats_from_link( $permalink, $post, $leavename ){
  3. $cats = get_the_category( $post->ID );
  4. if ( $cats ) {
  5. usort( $cats, '_usort_terms_by_ID' );
  6. $category = $cats[0]->slug;
  7. if ( $parent = $cats[0]->parent ) {
  8. $parentcats = get_category_parents( $parent, false, '/', true );
  9. $permalink = str_replace( $parentcats, '', $permalink );
  10. return $permalink;

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK