基于定制邮政类型的分类档案

时间:2016-11-22 作者:Cray

我已经生成了三种不同的自定义帖子类型(例如书籍、电影、游戏)。我还为它们创建了自定义分类法(例如流派)。

我需要的是基于帖子类型的分类的归档。例如:“书籍流派”,“电影流派”。。。

有什么解决办法吗?现在我只有“流派”的分类档案。

2 个回复
最合适的回答,由SO网友:Dave Romsey 整理而成

下面是一个完整的示例,可以使用add_rewrite_rule(). 首先记录本示例的基本设置,然后我们将使用add_rewrite_rule().

分类和后期类型注册genre 分类学和book, movie, 和game post类型(请注意,本示例中使用了这些名称的单数版本,因为它被认为是best practice)。

// Create taxonomy: genre for post types: book, movie, and game
// https://codex.wordpress.org/Function_Reference/register_taxonomy
add_action( \'init\', \'wpse247013_register_taxonomies\', 0 );
function wpse247013_register_taxonomies() {
    $args = [
        \'public\'            => true,
        \'hierarchical\'      => false,
        \'label\'             => __( \'Genres\', \'textdomain\' ),
        \'show_ui\'           => true,
        \'show_admin_column\' => true,
        \'query_var\'         => \'genre\',
        \'rewrite\'           => [ \'slug\' => \'genres\' ],
    ];

    register_taxonomy( \'genre\', [ \'book\', \'movie\', \'game\' ], $args );
}

// Create post types: movie, book, and game
// https://developer.wordpress.org/reference/functions/register_post_type/
add_action( \'init\', \'wpse247013_register_post_types\' );
function wpse247013_register_post_types() {
    $book_args = [
        \'label\'              => __( \'Books\', \'textdomain\' ),
        \'public\'             => true,
        \'publicly_queryable\' => true,
        \'show_ui\'            => true,
        \'show_in_menu\'       => true,
        \'query_var\'          => true,
        \'rewrite\'            => [ \'slug\' => \'books\' ],
        \'capability_type\'    => \'post\',
        \'has_archive\'        => true,
        \'hierarchical\'       => false,
        \'menu_position\'      => null,
        \'supports\'           => [ \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\' ],
        \'taxonomies\'         => [ \'genre\' ],
    ];
    register_post_type( \'book\', $book_args );

    $movie_args = [
        \'label\'             => __( \'Movies\', \'textdomain\' ),
        \'public\'             => true,
        \'publicly_queryable\' => true,
        \'show_ui\'            => true,
        \'show_in_menu\'       => true,
        \'query_var\'          => true,
        \'rewrite\'            => [ \'slug\' => \'movies\' ],
        \'capability_type\'    => \'post\',
        \'has_archive\'        => true,
        \'hierarchical\'       => false,
        \'menu_position\'      => null,
        \'supports\'           => [ \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\' ],
        \'taxonomies\'         => [ \'genre\' ],
    ];
    register_post_type( \'movie\', $movie_args );

    $game_args = [
        \'label\'             => __( \'Games\', \'textdomain\' ),
        \'public\'             => true,
        \'publicly_queryable\' => true,
        \'show_ui\'            => true,
        \'show_in_menu\'       => true,
        \'query_var\'          => true,
        \'rewrite\'            => [ \'slug\' => \'games\' ],
        \'capability_type\'    => \'post\',
        \'has_archive\'        => true,
        \'hierarchical\'       => false,
        \'menu_position\'      => null,
        \'supports\'           => [ \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\' ],
        \'taxonomies\'         => [ \'genre\' ],
    ];
    register_post_type( \'game\', $game_args );
}
基本URL示例基于以上设置,我们将立即获取这些URL:

Post type archive URLs

Single post type URLs

Taxonomy Term archive URLs

  • http://example.com/genres/{流派术语slug}
    (此存档将包括all 文章类型,这不是我们所追求的)必须添加重写规则处理重写规则,以便将特定体裁术语限制为单个文章类型。每种帖子类型的分页都需要一个额外的规则。

    /**
     * Add rewrite rules for genre terms limited to book, movie, and game post types.
     * Pagination issue fix via http://wordpress.stackexchange.com/a/23155/2807
     * @link https://codex.wordpress.org/Rewrite_API/add_rewrite_rule
     */
    function wpse247013_rewrite_rules() {
        // Book Genres
        add_rewrite_rule( \'^books/book-genres/([^/]+)/?$\',
                \'index.php?taxonomy=genre&post_type=book&term=$matches[1]\', \'top\' );
    
        // Book Genres pagination
        add_rewrite_rule( \'^books/book-genres/([^/]+)/page/([0-9]+)?$\',
                \'index.php?post_type=book&genre=$matches[1]&paged=$matches[2]\', \'top\' );
    
        // Movie Genres
        add_rewrite_rule( \'^movies/movie-genres/([^/]+)/?$\',
                \'index.php?taxonomy=genre&post_type=movie&term=$matches[1]\', \'top\' );
    
        // Movie Genres pagination
        add_rewrite_rule( \'^movies/movie-genres/([^/]+)/page/([0-9]+)?$\',
                \'index.php?post_type=movie&genre=$matches[1]&paged=$matches[2]\', \'top\' );
    
        // Game Genres
        add_rewrite_rule( \'^games/game-genres/([^/]+)/?$\',
                \'index.php?taxonomy=genre&post_type=game&term=$matches[1]\', \'top\' );
    
        // Game Genres pagination
        add_rewrite_rule( \'^games/game-genres/([^/]+)/page/([0-9]+)?$\',
                \'index.php?post_type=game&genre=$matches[1]&paged=$matches[2]\', \'top\' );
    }
    add_action( \'init\', \'wpse247013_rewrite_rules\', 10, 0 );
    
    确保通过访问刷新重写规则Settings > Permalinks 将此代码添加到插件或主题后。如果您使用的是插件,则可以使用register_activation_hook.

    自定义URL上面添加的重写规则将启用以下新URL:

SO网友:GKS

您可以像这样动态注册分类法,

add_action(\'init\',function(){

    $postType = array( \'books\',\'movies\',\'games\' );

    foreach ( $postType as $k => $cpt ) {

        $tax_slug = strtolower( $cpt ) . \'-genre\';

        register_taxonomy(
            $tax_slug,
            strtolower( $cpt ),
            array(
                    \'label\' => \'Genre\',
                    \'rewrite\' => array( \'slug\' => $tax_slug ),
                    \'hierarchical\' => true,
                )
        );
    }
});

相关推荐

Custom Taxonomy Page

我正在尝试创建一个显示特定类别的所有子类别的页面。所以在上下文中,我有一个标题为“目的地”的父类别。我希望能够点击目的地,并被引导到一个页面,显示子类别或国家的列表。下面是我试图实现的一个示例,减去顶部的地图-https://www.vagabrothers.com/destinations. 在理想情况下,类别页面的布局将与此页面相同。你可以从上面的例子中看出我的意思。它会使网站上的导航像这样:目的地>国家>个人帖子。我正在使用CPT UI,设置了一个名为“目的地”的自定义帖子类型和类别,然