具有自己的固定链接结构的自定义帖子类型

时间:2012-09-30 作者:Bram Vanroy

我的主要帖子是新闻(新闻类)。因此,我使用以下格式编写全局URI:/year/month/post-name

我也有包含相册(类别“照片”)的帖子,但我希望这些帖子具有另一个URI结构,即:/category/post-name 属于哪个类别photos

我在网上查了一下,发现this link (有类别,但我认为in category 更好)包含相当多的信息。我还模糊地记得函数中的一个钩子。php可以做到这一点:

\'rewrite\' => ...
不幸的是,对于像我这样的Wordpress新手来说,这还不够。我不知道怎么做。有什么帮助吗?谢谢

因此,如果类别为新闻=>/year/month/post-name e、 g。/2012/10/test-post如果其他类别,如照片或视频=>/category/[sub-category/)post-name e、 g。/photos/(2012/)France 或用于视频:/videos/(2011/)concert-u2

编辑:评论告诉我使用自定义帖子类型更明智videos 和photos). 我想不会有其他的了。需要做的事情:更改自定义帖子类型的permalink结构。我希望他们是/photos/post-name/videos/post-name. 常规岗位应为/year/month/post-name. 我将此添加到我的函数中。php

/* Add custom post-type (fotos) (added by Bram Vanroy) */

add_action( \'init\', \'register_cpt_foto\' );

function register_cpt_foto() {

    $labels = array( 
        \'name\' => _x( \'fotos\', \'foto\' ),
        \'singular_name\' => _x( \'foto\', \'foto\' ),
        \'add_new\' => _x( \'Nieuw fotoalbum\', \'foto\' ),
        \'add_new_item\' => _x( \'Voeg nieuw fotoalbum toe\', \'foto\' ),
        \'edit_item\' => _x( \'Bewerk fotoalbum\', \'foto\' ),
        \'new_item\' => _x( \'Nieuw fotoalbum\', \'foto\' ),
        \'view_item\' => _x( \'Bekijk fotoalbum\', \'foto\' ),
        \'search_items\' => _x( \'Zoek in fotoalbums\', \'foto\' ),
        \'not_found\' => _x( \'Geen fotoalbums gevonden\', \'foto\' ),
        \'not_found_in_trash\' => _x( \'Geen fotoalbums gevonden in de prullenmand\', \'foto\' ),
        \'parent_item_colon\' => _x( \'Parent foto:\', \'foto\' ),
        \'menu_name\' => _x( \'Foto\\\'s\', \'foto\' ),
    );

    $args = array( 
        \'labels\' => $labels,
        \'hierarchical\' => true,
        \'description\' => \'Het posttype dat alle foto-albums verzorgt.\',
        \'supports\' => array( \'title\', \'editor\', \'excerpt\', \'comments\', \'page-attributes\' ),
        \'taxonomies\' => array( \'post_tag\', \'page-category\' ),
        \'public\' => true,
        \'show_ui\' => true,
        \'show_in_menu\' => true,


        \'show_in_nav_menus\' => true,
        \'publicly_queryable\' => true,
        \'exclude_from_search\' => false,
        \'has_archive\' => true,
        \'query_var\' => true,
        \'can_export\' => true,
        \'rewrite\' => true,
        \'capability_type\' => \'page\'
    );

    register_post_type( \'foto\', $args );
}

/* Add custom post-type (videos) (added by Bram Vanroy) */
add_action( \'init\', \'register_cpt_video\' );

function register_cpt_video() {

    $labels = array( 
        \'name\' => _x( \'videos\', \'video\' ),
        \'singular_name\' => _x( \'video\', \'video\' ),
        \'add_new\' => _x( \'Voeg nieuwe video toe\', \'video\' ),
        \'add_new_item\' => _x( \'Voeg nieuwe video toe\', \'video\' ),
        \'edit_item\' => _x( \'Bewerk video\', \'video\' ),
        \'new_item\' => _x( \'Nieuwe video\', \'video\' ),
        \'view_item\' => _x( \'Bekijk video\', \'video\' ),
        \'search_items\' => _x( \'Zoek video\\\'s\', \'video\' ),
        \'not_found\' => _x( \'Geen video\\\'s gevonden\', \'video\' ),
        \'not_found_in_trash\' => _x( \'Geen video\\\'s gevonden in de prullenmand\', \'video\' ),
        \'parent_item_colon\' => _x( \'Parent video:\', \'video\' ),
        \'menu_name\' => _x( \'Video\\\'s\', \'video\' ),
    );

    $args = array( 
        \'labels\' => $labels,
        \'hierarchical\' => true,
        \'description\' => \'Dit posttype wordt gebruikt om filmpjes te publiceren\',
        \'supports\' => array( \'title\', \'editor\', \'excerpt\', \'comments\', \'page-attributes\' ),
        \'taxonomies\' => array( \'post_tag\', \'page-category\' ),
        \'public\' => true,
        \'show_ui\' => true,
        \'show_in_menu\' => true,


        \'show_in_nav_menus\' => true,
        \'publicly_queryable\' => true,
        \'exclude_from_search\' => false,
        \'has_archive\' => true,
        \'query_var\' => true,
        \'can_export\' => true,
        \'rewrite\' => true,
        \'capability_type\' => \'page\'
    );

    register_post_type( \'video\', $args );
}
我想我需要一些重写规则,但我不知道怎么做!

1 个回复
最合适的回答,由SO网友:Bram Vanroy 整理而成

我自己解决了(在你们的帮助下):这很容易:

\'rewrite\' => array(
            \'slug\' => \'videos\',
            \'with_front\' => false
        ),

结束