首页 文章

Wordpress:如何将其父级添加到自定义帖子类型子页面作为slug?

提问于
浏览
1

将“hierarchical”=> true设置为自定义帖子类型并将父级分配给具有此自定义帖子类型的页面时会抛出404.可以使这个工作吗?

预期结果:http://example.com/parent-page/page-title/

这适用于Wordpress普通页面,但不适用于自定义帖子类型(未找到404) .

提前致谢!

1 回答

  • 2

    由于我刚刚注意到您想要一个与页面完全相同的永久链接结构,因此您可以在注册帖子类型时修改 rewrite 参数 . 这是一个如何使它像页面结构一样的例子:

    $args = array(
        'labels'              => $labels,
        'hierarchical'        => true, // just to get parent child relationships with your custom post [but you already knew this part]
        'description'         => 'Your custom post type description here',
        'has_archive'         => false,
        'rewrite'             => array('slug' => '/'), // we rewrite the slug to the root url so that it has no post type prefix
        'with_front'          => false, // This is optional but just in case your posts are set to /blog/post-name this will make it /post-name for your custom post type 
        'capability_type'     => 'post',
        'supports'            => array(
            'title', 'editor', 'author', 'thumbnail', 'page-attributes'
            )
    );
    
    register_post_type( 'post_type', $args );
    

    另外,请确保在进行这些更改后更新永久链接结构,以便Wordpress可以重写规则 .

相关问题