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

问题描述

添加了 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;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)