【问题标题】:Wordpress: How can I add to a custom post type child page its parent as slug?Wordpress:如何将其父页面作为 slug 添加到自定义帖子类型子页面?
【发布时间】:2016-06-28 14:29:21
【问题描述】:

当将 'hierarchical' => true 设置为自定义帖子类型并将父级分配给具有此自定义帖子类型的页面时,会抛出 404。是否可以进行此操作?

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

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

提前致谢!

【问题讨论】:

  • 这个永久链接结构可以吗:example.com/post-type/parent-slug/child-slug?还是您根本不想要永久链接中的帖子类型?
  • 我不希望永久链接中的帖子类型 :(
  • 我刚刚更新了我的答案,因为它很清楚:)

标签: wordpress


【解决方案1】:

由于我刚刚注意到您想要一个与页面完全相同的永久链接结构,因此您可以在注册帖子类型时修改 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 可以重写规则。

【讨论】:

    猜你喜欢
    • 2017-08-26
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多