url中的类别和子类别自定义帖子类型

时间:2021-03-23 17:54:53

标签: php wordpress taxonomy-terms

我添加了 cpt 和自定义分类法。我在我的functions.php 中编写了函数,它添加了我的网址、类别和子类别,然后我的网址是“mysite.com/house/category/subcategory/post_name”。 Url 显示但不起作用,因为显示消息“找不到页面”。如果我在我的 url 或子类别中删除类别,则显示此单页。请帮我!我的代码:

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%')) {
        $terms = get_the_terms($post->ID, 'kategorie_dladomu');

        foreach ( $terms as $term ) {
            if ( $terms->parent == 0 ){
                $category = $term->slug;
                foreach( $terms as $subterms ){
                    if ( $subterms->parent == $term->term_id ){
                        $subcategory = $subterms->slug;
                    }
                }
            }
        }
        $slug = "$category/$subcategory";
        if ( ! empty( $slug ) ) {
            return str_replace( '%kategorie_dladomu%' , $slug , $post_link );
        }
    }
    return $post_link;
}

0 个答案:

没有答案
相关问题