【问题标题】:Wordpress custom post types with child pages带有子页面的 Wordpress 自定义帖子类型
【发布时间】:2021-06-17 03:22:01
【问题描述】:

我很难找到解决问题的方法。

我们在 wordpress 中有一个名为“stores”的自定义帖子类型,每个商店都有一个唯一的名称,例如“LA store, NY Store”,每个商店都有子页面,例如位置、营业时间等。

但不幸的是,为每个商店创建所有子页面是一项艰巨的工作。相反,我们想为子页面使用一些模板。

我们希望我们的 url 结构如下:

sample.com/store/lastore/opening-hours

【问题讨论】:

  • 你能说得更具体些吗?您想要替代子页面系统吗?
  • 嗨 amarinediary 首先感谢您的编辑 :) 基本上我们不想在 wordpress 中为每个商店创建每个子页面,而是我们希望在用户尝试访问时创建商店“ sample.com/store/lastore/opening-hours”它不应该转到 404,而是包含一个模板部分
  • 模板部分正是它们的本质,部分。它们不是模板。您不能将一个用作页面。但是,考虑一下您可以automatically generate a child page after the save_post action is fired upon publish a parent page。然后您可以为该页面设置一个页面模板,并从父 slug 生成应该在其上的任何数据。 (例如:获取自定义元框值等)...

标签: php wordpress url seo structure


【解决方案1】:

为此,您可以在注册帖子类型时修改重写参数。下面是一个例子:

$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 可以重写规则。

【讨论】:

    猜你喜欢
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-11
    • 2014-05-20
    • 1970-01-01
    • 2016-04-01
    相关资源
    最近更新 更多