WordPress教程

WordPress自定义文章类型支持选择父级文章

阿里云

我们都知道 WordPress 自带的页面文章类型中是可以选择父级别文章(页面)的,这个功能其实在我们做 WordPress 主题开发中有时还是很有用处的,比如我们开发书籍或者在线课堂类的主题时,我们就可以使用父级文章或者说子文章来实现书名-章节的逻辑关系,那么当我们创建自定义文章类型的时候怎么样才能支持父级文章选择功能呢?其实很简单的,我们只需要在 supports 参数中添加上 page-attributes(页面属性)这样一个参数就行了。比如下面的创建了一个课程的自定义文章类型。

  1. /**创建课程自定义文章类型**/
  2. function my_custom_post_course() {
  3.   $labels = array(
  4.     'name'               => _x( '课程', 'post type 名称' ),
  5.     'singular_name'      => _x( '课程', 'post type 单个 item 时的名称,因为英文有复数' ),
  6.     'add_new'            => _x( '添加新课程', '添加新内容的链接名称' ),
  7.     'add_new_item'       => __( '添加新课程' ),
  8.     'edit_item'          => __( '编辑课程' ),
  9.     'new_item'           => __( '新课程' ),
  10.     'all_items'          => __( '所有课程' ),
  11.     'view_item'          => __( '预览课程' ),
  12.     'search_items'       => __( '搜索课程' ),
  13.     'not_found'          => __( '没有找到课程' ),
  14.     'not_found_in_trash' => __( '回收站没有找到课程' ),
  15.     'parent_item_colon'  => '父级',
  16.     'menu_name'          => '课程'
  17.   );
  18.   $args = array(
  19.     'labels'        => $labels,
  20.     'description'   => '课程管理',
  21.     'public'        => true,
  22.     'menu_position' => 5,
  23.     'hierarchical'  => true,
  24.     'supports'      => array('title','editor','author','thumbnail','excerpt','comments','page-attributes'),
  25.     'has_archive'   => false,
  26.   );
  27.   register_post_type( 'course', $args );
  28. }
  29. add_action( 'init', 'my_custom_post_course' );
也想出现在这里?联系我们
创客主机

WordPress 自定义文章类型支持选择父级文章

已有 206 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!