自定义邮寄类型美观结构

时间:2014-12-18 作者:Lars

我正在用自定义的帖子类型自定义wordpress安装,但permalink结构有问题。我添加了一个team_member CPT,并且内容应在特定URL中可见:/about-us/team/%team_member%/. 安装的其他页面应如下所示/%pagename%/ (例如。/about-us/), 而博客帖子应该在/blog/%postname%/.

当permalink结构设置为默认值时,页面会工作,从而产生如下链接example.com/?team_member=john_doe. 当我激活永久链接格式时/blog/%postname, 页面和博客帖子确实有效,但team\\u成员CPT根本不起作用。在没有任何重写结构的情况下,team\\u成员位于/blog/team_member/john_doe/, 导致404。

即使使用自定义重写结构,也会发生同样的情况。目前(正在开发中),我每次通过注册CPT后都会刷新重写缓存flush_rewrite_rules( true );. 以下是注册我的CPT,通过after_setup_theme 挂钩:

/**
 * Register Custom Post Type
 */
function register_cpt_team_member() {

    $labels = array(
        \'name\'                => _x( \'Team Members\', \'Post Type General Name\', \'client\' ),
        // [...]
    );
    $args = array(
        \'label\'               => __( \'team_member\', \'client\' ),
        \'description\'         => __( \'Team Members\', \'client\' ),
        \'labels\'              => $labels,
        \'supports\'            => array( \'title\', \'editor\', \'thumbnail\' ),
        \'hierarchical\'        => false,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'menu_position\'       => 5,
        \'can_export\'          => true,
        \'has_archive\'         => true,
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
        \'capability_type\'     => \'page\',
        \'rewrite\' => array (
            \'slug\' => \'/about-us/team/\',
            \'with_front\' => false
        )
    );
    register_post_type( \'team_member\', $args );

    global $wp_rewrite;
    $permalink_structure = \'/about-us/team/%team_member%/\';
    $wp_rewrite->add_rewrite_tag("%team_member%", \'([^/]+)\', "team_member=");
    $wp_rewrite->add_permastruct(\'team_member\', $permalink_structure, false);
}
我不明白,为什么既没有也没有重写的permalink结构

1 个回复
SO网友:cybmeta

我已经为帖子配置了permastructure/blog/%postname%/, 我用slug创建了一个页面about-us 我用下面的代码注册team_member CPT正在运行。我想你在重写的争论中多了一个“/”。此外,您不需要与global $wp_rewrite; 如果在注册post类型时设置重写参数,WordPress将为您创建重写规则。为了保存不需要的代码,我也发表了一些评论:

add_action("init", "register_cpt_team_member");
function register_cpt_team_member() {

    $labels = array(
        //If "label" is set, the "name" in "labels" argument is usually not needed
        //\'name\'                => _x( \'Team Members\', \'Post Type General Name\', \'client\' ),
        // [...]
    );
    $args = array(
        //label should a plural descriptive name
        \'label\'               => __( \'Team Members\', \'client\' ),
        //description should include ...... a description ;)
        \'description\'         => __( \'Custom post type to manage team members\', \'client\' ),
        //As the only "labels" set was "name" and it overriden by general "label" you don\'t need this
        //\'labels\'              => $labels,
        \'supports\'            => array( \'title\', \'editor\', \'thumbnail\' ),
        //hierarchical is by default false, you don\'t need this
        //\'hierarchical\'      => false,
        \'public\'              => true,
        //show_ui is by default set to same value of "public", so you don\'t need this
        //\'show_ui\'             => true,
        //show_in_menu by default set to same value of "show_ui", so you don\'t need this
        //\'show_in_menu\'             => true,
        //show_in_nav_menus is by default set to same value of "public", so you don\'t need this
        //\'show_in_nav_menus\'   => true,
        //show_in_admin_bar by default set to same value of "show_in_menu", so you don\'t need this
        //\'show_in_admin_bar\'   => true,
        \'menu_position\'       => 5,
        //can_export is by default set to true, so you don\'t need this
        //\'can_export\'          => true,
        \'has_archive\'         => true,
        //exclude_from_search is by default set to opposite of "public", so you don\'t need this
        //\'exclude_from_search\' => false,
        //publicly_queryable is by default set to same value of "public" argumente, so you don\'t need this
        //\'publicly_queryable\'  => true,
        \'capability_type\'     => \'page\',
        \'rewrite\' => array (
            \'slug\' => \'/about-us/team\',
            \'with_front\' => false
        )
    );

    register_post_type( \'team_member\', $args );

}
因此,可以这样编写上述代码,并得到相同的结果:

add_action("init", "register_cpt_team_member");
function register_cpt_team_member() {

    $args = array(
        \'label\'               => __( \'Team Members\', \'client\' ),
        \'description\'         => __( \'Custom post type to manage team members\', \'client\' ),
        \'supports\'            => array( \'title\', \'editor\', \'thumbnail\' ),
        \'public\'              => true,
        \'menu_position\'       => 5,
        \'has_archive\'         => true,
        \'capability_type\'     => \'page\',
        \'rewrite\' => array (
            \'slug\' => \'/about-us/team\',
            \'with_front\' => false
        )
    );

    register_post_type( \'team_member\', $args );

}

结束

相关推荐

Custom permalinks structure

我希望有这样的结构:www.mysite.com/2013 (必须显示2013年的所有职位)www.mysite.com/my-category/2013 (必须显示2013年和“我的类别”类别的所有帖子)www.mysite.com/my-category/my-tag/ (必须显示所有类别为“我的类别”和标记为“我的标记”的帖子)www.mysite.com/my-category/ (必须显示“我的类别”类别的所有帖子)www.mysite.com/my-