WordPress文章链接中的父级分类名怎么删除

追格官方小助手/ 2022年02月24日/ WordPress/ 浏览 1777

追格小程序配置过程中都要配置固定链接/%category%/ ,但链接又特别长,不太好看,比如:


比如:https://www.zhuige.com/888/999/1.html,888是父分类,999是子分类。


如果想把父分类删除,可以将下面代码添加到当前WordPress模板函数functions.php中:


add_filter( 'post_link', 'zhuige_remove_parent_cats_from_link', 10, 3 );
    function zhuige_remove_parent_cats_from_link( $permalink, $post, $leavename ){
        $cats = get_the_category( $post->ID );
        if ( $cats ) {
            usort( $cats, '_usort_terms_by_ID' );
            $category = $cats[0]->slug;
        if ( $parent = $cats[0]->parent ) {
            $parentcats = get_category_parents( $parent, false, '/', true );
            $permalink = str_replace( $parentcats, '', $permalink );
        }
    }
    return $permalink;
}


发表评论

暂无评论,抢个沙发...

客服 工单