0

我注册了自定义帖子类型并注册了自定义分类法。我添加了类别“Hormann”,并添加了子类别“Bramy”。我想在我的帖子“mysite.com/archive_name/category/subacategory/post_name”中有网址。请帮我。

编辑:我确实在我的文件functions.php中编写了代码,但我的代码无法正常工作,因为进入类别“mysite.com/archive_name/category/”后显示链接,但点击后显示错误链接帖子“mysite.com/archive_name/subacategory/post_name”

add_filter('post_type_link', 'projectcategory_permalink_structure', 10, 4);
function projectcategory_permalink_structure($post_link, $post, $leavename, $sample) {
    if (false !== strpos($post_link, '%kategorie_dladomu%')) {
        $projectscategory_type_term = get_the_terms($post->ID, 'kategorie_dladomu');
        $slug = [];
        foreach ( $projectscategory_type_term as $project ){
            if ( $project->parent == 0 ) {
                array_unshift( $slug, sanitize_title_with_dashes( $project->name ) );
            } else {
                array_push( $slug, sanitize_title_with_dashes( $project->name ) );
            }
            if ( ! empty( $slug ) ) {
                return str_replace( '%kategorie_dladomu%' , join( '/', $slug ) , $post_link );
            }
        }
    }
    return $post_link;
}

4

0 回答 0