自定义帖子类型中的父子关系


10

如何在自定义帖子类型中维护父子关系,以便具有统一的URL结构?我想将网址结构设为4个级别,例如

example.com/sponsor-child/disadvantaged-community/gita-magar

没有插件有可能吗?当我转到单个页面URL时,它以3个级别结尾。

Answers:


15

register_post_type通话中,请确保您具有以下参数:

register_post_type(
    'my_post_type',
    array(
        'hierarchical' => true,
        'public' => true,
        'rewrite' => array(
            'slug'       => 'my_post_type',
            'with_front' => false,
        ),
        'supports' => array(
            'page-attributes' /* This will show the post parent field */,
            'title',
            'editor',
            'something-else',
        ),
        // Other arguments
    )
);

确保您的永久链接已清除(只需访问“设置”>“永久链接”页面)。

现在,当您创建一个new时my_post_type,只需将其父级设置为另一个父级,它的永久链接将类似于:

http://example.com/parent-post-type/my-post-type/

您可以根据需要选择多个级别。


1
能给我完整的代码来创建带有父子关系的自定义帖子类型,并将URL级别设置为4个级别,以使URL的结构统一。
user3445146

对我来说效果很好-从技术上讲,应该没有任何限制。您是否在全新安装的WordPress中测试了此功能,并禁用了所有插件,然后刷新了永久链接?
TheDeadMedic 2015年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.