【问题标题】:2 Taxonomies with same slug2 具有相同蛞蝓的分类法
【发布时间】:2021-03-10 05:35:10
【问题描述】:

我有 2 个自定义分类法,它们共享相同的 slug 并有一个自定义帖子,名为 Courses

1:

'rewrite' => [ 'slug' => 'courses/%tag%', 'with-front' => false ],
'labels'  => [ 'name' => 'Tag' ] 

2:

'rewrite' => [ 'slug' => 'courses/%cat%', 'with-front' => 'false' ],
'labels'  => [ 'name' => 'Category' ]

我使用 类别分类 重写了自定义帖子类型的 URL,以便我可以拥有此 URL

courses/math/my-post 

它有效,即使我去上课程/数学时,我也会看到我所有的数学帖子。

courses/math

但我也有标签,我希望它们与类别处于同一级别但我得到 404,例如:

courses/logic

我怎样才能使这两种分类法可以在同一水平上共同存在?

这是我对自定义帖子类型的重写规则。

function courses_post_link( $post_link, $id = 0 ){
    $post = get_post($id);  
    if ( is_object( $post ) ){
        $terms = wp_get_object_terms( $post->ID, 'course_category' );
        if( $terms ){
            return str_replace( '%cat%' , $terms[0]->slug , $post_link );
        }
     }
     return $post_link;  
 }
 add_filter( 'post_type_link', 'courses_post_link', 1, 3 );


 function archive_rewrite_rules() {
     add_rewrite_rule(
        '^courses/(.*)/(.*)/?$',
        'index.php?post_type=courses&name=$matches[2]',
        'top'
     );

}

【问题讨论】:

    标签: wordpress url-rewriting custom-taxonomy taxonomy-terms


    【解决方案1】:

    已解决。

    我使用了一个名为 Permalink Customizer 的插件。

    【讨论】:

      猜你喜欢
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      • 2019-08-15
      • 2020-02-21
      • 2012-08-01
      • 2013-03-24
      • 1970-01-01
      • 2012-09-30
      相关资源
      最近更新 更多