WordPress自定义分类法分层术语重写

时间:2015-09-07 16:16:14

标签: wordpress rewrite taxonomy

我创建了一个自定义分类URL结构,如下所示:

/ term_slug / POST_NAME

我已经创建了自定义重写规则,如下所示:

register_taxonomy( 'blog_type', 'post', array(
        'labels' => array(
            'name'              => _x( 'Blog Types', 'taxonomy general name' ),
            'singular_name'     => _x( 'Blog Type', 'taxonomy singular name' ),
            'search_items'      => __( 'Search Blog Types' ),
            'all_items'         => __( 'All Blog Types' ),
            'parent_item'       => __( 'Parent Blog Type' ),
            'parent_item_colon' => __( 'Parent Blog Type:' ),
            'edit_item'         => __( 'Edit Blog Type' ),
            'update_item'       => __( 'Update Blog Type' ),
            'add_new_item'      => __( 'Add New Blog Type' ),
            'new_item_name'     => __( 'New Blog Type Name' ),
            'menu_name'         => __( 'Blog Type' ),
        ),
        'rewrite' => false,
        'hierarchical' => true,
    ) );

    add_rewrite_tag( "%blog_type%", '(.+?)', "blog_type=" );


    // Get all the blog types
    $blog_types = get_terms( 'blog_type', array( 'hide_empty' => false, ) );

    if( ! empty( $blog_types ) ) {
        foreach( $blog_types as $blog_type ) {
            add_rewrite_rule( "$blog_type->slug/?$", "index.php?post_type=post&taxonomy=blog_type&term=$blog_type->slug", 'top' );
            add_rewrite_rule( "$blog_type->slug/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=post&taxonomy=blog_type&term=$blog_type->slug&paged=$matches[1]", 'top' );

            add_permastruct( "%$blog_type->slug%", "$blog_type->slug/%postname%", false );
        }
    }

除了我需要在URL结构中包含子术语之外,这种方法也很好用,所以例如:

/ term_slug / term_subterm_slug / POST_NAME

这将包括多维分类法,其中术语具有子术语。我现在有点陷入困境,有什么帮助吗?

我查看了this post,但没有详细说明如何使用动态数量的子术语创建网址结构。

0 个答案:

没有答案