【问题标题】:Wordpress custom post parent slugWordpress 自定义帖子父 slug
【发布时间】:2019-09-06 14:56:01
【问题描述】:

构建父/子自定义帖子类型组合的最佳方法是什么,以便我的 slug url 看起来像这样

https://www.mysite.co.uk/system/parent/child

slug 的 system 部分至关重要,它是 wordpress 多主题插件的一部分。此外,url 的父/子部分并不总是相同的。假设我做了一个名为“如何骑自行车”的课程,其中包含一个名为“系列 1”的系列,我的 slug 需要看起来像这样

https://www.mysite.co.uk/system/how-to-ride-a-bike/series-1

目前我的自定义帖子类型如下所示

<?php
function create_post_type() {
    //Courses
    register_post_type( 'courses',
      [
        'labels' => array(
          'name' => __( 'Courses' ),
          'singular_name' => __( 'Course' )
        ),
        'description' => 'All courses',
        'public' => true,
        'rewrite' => array(
            'slug'       => 'system',
        ),
        'menu_icon' => 'dashicons-welcome-learn-more',
        'supports' => ['title', 'custom_fields', 'page-attributes']
      ]
    );

    //Series
    register_post_type( 'series',
        [
          'labels' => array(
            'name' => __( 'Series' ),
            'singular_name' => __( 'Series' )
          ),
          'show_ui'              => true,
          'show_in_menu'         => 'edit.php?post_type=courses',
          'description' => 'Course series',
          'public' => true,
          'rewrite'             => array('slug' => '/'),
          'hierarchical'        => true,
          'with_front'          => false,
          'capability_type'     => 'post',
          'has_archive'         => false,
          'supports' => ['title', 'custom_fields', 'page-attributes']
        ]
      );
}
add_action( 'init', 'create_post_type' );
?>

我不能 100% 确定将子元素实际链接到父元素的最佳方式?目前,我在子帖子类型(系列)上有一个高级自定义字段,您可以在其中选择与其链接的父(课程)。

构建自定义帖子类型的最佳方法是什么,以便我可以将孩子链接到父母并指定上面的 slug?

【问题讨论】:

  • WordPress 不支持将一种文章类型作为另一种文章类型的子类型。他们都需要是帖子类型吗? “系统”真的只是系列的“容器”吗?通常你要么让它们都是相同的帖子类型,和/或你会用分类法来管理这种事情。如果 URL 重写是您的主要目标,而帖子类型是您尝试达到的目标,那么我建议您使用分类法(自定义的“分层”分类法)或利用 Rewrite API
  • 如果我将它们都作为相同的帖子类型进行,有没有办法可以在菜单中添加“课程”,然后当您创建课程并单击它时,我可以选择制作系列列表(与帖子/页面相同的列表样式)?
  • @S_R 可能您可以进行分层分类,而不是类似于类别和子类别的帖子。
  • 如果你把它们都设为相同的帖子类型,你可以让“父”帖子列出他们的“子”帖子,是的。因此,父母“课程”可以列出孩子的“课程”,是的。我鼓励您使用自定义分类(分层)来实现您所追求的 url 结构(“类别”是分层分类,其中一个类别可以是另一个类别的子类别 - “标签”不是,有没有与标签的父/子关系)

标签: php wordpress custom-post-type


【解决方案1】:

您可以使用supports 数组中的page-attributes 值来使用相同自定义帖子类型的父/子结构。

在添加子项之前,您确实需要刷新 Wordpress 的重写规则。只需访问永久链接页面并点击保存几次。

这是我使用自定义帖子类型subcase 的示例:

register_post_type( 'subcase',
    array(
        'hierarchical' => true, // needs to be true
        'labels'      => array(
            'name'          => __( 'Sub Cases' ),
            'singular_name' => __( 'Sub Case' )
        ),
        'taxonomies'  => array( 'category', 'post_tag' ),
        'public'      => true,
        'has_archive' => false,
        'rewrite'     => array(
            'slug'       => 'cases/subcases',
            'with_front' => true
        ),
        'supports'    => array(
            'page-attributes', // creates parent select box
            'title',
            'editor',
            'excerpt',
            'thumbnail'
        )
    )
);

【讨论】:

    猜你喜欢
    • 2017-01-31
    • 2015-08-12
    • 2017-02-28
    • 2017-03-30
    • 2013-10-28
    • 2014-10-18
    • 2014-02-08
    • 2019-09-12
    • 1970-01-01
    相关资源
    最近更新 更多