如何为自定义帖子类型实现不同的永久链接结构?

时间:2021-05-04 作者:geochanto

尝试使用此插件,但似乎不起作用:https://wordpress.org/plugins/custom-post-type-permalinks/

我正在设置设置中的博客帖子的永久链接结构>;像这样的永久链接:/blog/%postname%

我还有一个自定义的post类型,名为;“项目”;,我想要结构的位置/our-work/%postname, 但相反/blog 部分也在这里预先准备好了。如何为这种自定义帖子类型保留单独的永久链接结构?

这是register_post_type 功能:

register_post_type(\'project\', array(
    \'public\' => true,
    \'custom\' => true,
    \'show_ui\' => true,
    \'supports\' => array(\'title\',\'editor\',\'revisions\'),
    \'labels\' => array(
        \'name\' => \'Projects\',
        \'singular_name\' => \'Project\',
        \'add_new_item\' => \'Add New Project\',
        \'edit_item\' => \'Edit Project\'
    ),
    \'menu_icon\' => \'dashicons-portfolio\',
    \'show_ui\' => true,
    \'show_in_menu\' => true,
    \'show_in_rest\' => true,
    \'rewrite\' => array(
        \'slug\' => \'our-work\', 
        \'with_front\' => true
    ),
    \'has_archive\' => true,
    \'show_in_graphql\' => true,
    \'graphql_single_name\' => \'project\',
    \'graphql_plural_name\' => \'projects\',
    \'cptp_permalink_structure\' => \'%post_id%\'
));

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

只需设置with_front 参数到false:

register_post_type( \'project\', array(
    ... your args.

    \'rewrite\' => array(
        \'slug\'       => \'our-work\',
        \'with_front\' => false
    ),

    ... your args.
) );