5ucms论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 9401|回复: 0
打印 上一主题 下一主题

[钩子/函数] register_post_type()函数 自定义文章类型

[复制链接]

670

主题

785

帖子

8343

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
8343
跳转到指定楼层
楼主
发表于 2021-6-1 10:45:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
可不学,因为有pods插件就够用了
----------------------------------------

语法结构

register_post_type($post_type,$args)
参数

$post_type (string) (必选) 文章类型的名称(最多20个字符) Default: 空
$args (array) (可选) 一个数组参数 Default: 空

Post Type 可以自定义的功能非常多,所以这个函数里面的 $args参数会很多。所以通常会用下面这种格式来注册,也有很多是可选项:

  1. // 注册自定义文章形式
  2. function custom_post_type(){
  3.     $labels = array(
  4.         'name' => 'Post Type General Name',
  5.         'singular_name' => 'Post Type Singular Name',
  6.         'menu_name' => 'Post Types',
  7.         'name_admin_bar'  => 'Post Type',
  8.         'archives'  => 'Item Archives',
  9.         'attributes' => 'Item Attributes',
  10.         'parent_item_colon' => 'Parent Item:',
  11.         'all_items' => 'All Items',
  12.         'add_new_item' => 'Add New Item',
  13.         'add_new' => 'Add New',
  14.         'new_item' => 'New Item',
  15.         'edit_item' => 'Edit Item',
  16.         'update_item' => 'Update Item',
  17.         'view_item' => 'View Item',
  18.         'view_items' => 'View Items',
  19.         'search_items' => 'Search Item',
  20.         'not_found' => 'Not found',
  21.         'not_found_in_trash' => 'Not found in Trash',
  22.         'featured_image' => 'Featured Image',
  23.         'set_featured_image' => 'Set featured image',
  24.         'remove_featured_image' => 'Remove featured image',
  25.         'use_featured_image' => 'Use as featured image',
  26.         'insert_into_item' => 'Insert into item',
  27.         'uploaded_to_this_item' => 'Uploaded to this item',
  28.         'items_list' => 'Items list',
  29.         'items_list_navigation' => 'Items list navigation',
  30.         'filter_items_list' => 'Filter items list'
  31.     );
  32.     $args = array(
  33.         'labels' => $labels,
  34.         'description' => 'Post Type Description',
  35.         'supports'  => array('title','editor','author','thumbnail','excerpt','comments'),
  36.         'taxonomies' => array( 'category', 'post_tag' ),
  37.         'hierarchical' => false,
  38.         'public' => true,
  39.         'show_ui' => true,
  40.         'show_in_menu' => true,
  41.         'menu_position' => 5,
  42.         'menu_icon' => 'dashicons-cart',
  43.         'show_in_admin_bar' => true,
  44.         'show_in_nav_menus' => true,
  45.         'can_export' => true,
  46.         'has_archive' => true,
  47.         'exclude_from_search' => false,
  48.         'publicly_queryable' => true,
  49.         'capability_type' => 'page',
  50.     );
  51.     register_post_type('post_type', $args);
  52. }
  53. add_action('init', 'custom_post_type');
复制代码

$labels用来配置文章类型显示在后台的一些描述性文字,默认为空。(上面代码中,为了清晰所以单独拿出来创建了一个数组 $labels)

name – 文章类型的名称(英语写为复数)
singular_name – 单篇文章类型的名称(英语写为复数)
add_new – 对应“添加新的文本”
all_items – 子菜单的字符串。默认是所有帖子/所有页面。
add_new_item – “添加新帖/新页面”
edit_item – “编辑帖子/页面”
new_item – “新贴/新页”
view_item – 用于查看帖子类型归档的标签。默认是’查看帖子’/’查看页面’
search_items – 默认是搜索帖子/搜索页面
not_found – 默认是没有发现帖子/找不到页面。
not_found_in_trash – 默认是在垃圾桶中找不到帖子/在垃圾桶中找不到页面。
parent_item_colon – 此字符串不用于非分层类型。在层次结构中,默认为“父页面:”。
menu_name’ – 菜单名称,默认与`name`相同。

$args的详细参数

description – 一些简短的介绍文字
public- 用于定义publicly_queryable,exclude_from_search,show_ui,show_in_nav_menus可见的方式, 默认是false,’true 的话即为show_ui = true,public_queryable = true,exclude_from_search = false,show_in_nav_menus = true

publicly_queryable – 可以从前台获取的变量(从url中,比如url重写),默认值:public参数的值
exclude_from_search – 是否能够被搜索到。默认值:与public参数相反的值
show_ui – 是否生成一个默认的管理页面,也就是是否在后台有管理页面。默认值:public参数的值
show_in_nav_menus -是否可以在导航菜单中选择post_type。默认值:public参数的值
show_in_menu- 是否在后台菜单项中显示,如果为ture,那么show_ui的值也必须设置为true,将会有一个顶级菜单项。 默认值:null
menu_position – 在后台菜单中的位置

5 – below Posts
10 – below Media
15 – below Links
20 – below Pages
25 – below comments
60 – below first separator
65 – below Plugins
70 – below Users
75 – below Tools
80 – below Settings
100 – below second separator
menu_icon-用于此菜单的图标的URL或iconfont中图标的名称  默认值:null – 默认为帖子图标
capability_type – 查看、编辑、删除的能力类型(capability),默认为post
capabilities – 这个帖子类型的功能的数组 (一般人用不到)。默认值:capability_type用于构造
map_meta_cap – 是否使用内部默认元功能处理,只有设置了capabilities才用的上。默认值:false
hierarchical – 文章是否有层级关系,也就是是否允许有父级文章。
supports – 对文章类型的一些功能支持

‘title’ 标题
‘editor’ (content) 编辑
‘author’ 作者
‘thumbnail’ 特色图
‘excerpt’ 摘抄
‘trackbacks’ 引用通过
‘custom-fields’ 自定义字段
‘comments’ 评论
‘revisions’ 修订版
‘page-attributes’ 页面属性,类似page,选择页面模板的那个
register_meta_box_cb – 提供在设置编辑表单的元框时调用的回调函数。回调函数使用一个参数$ post,其中包含当前编辑的帖子的WP_Post对象。在回调中执行remove_meta_box()和add_meta_box()调用。默认值:无
taxonomies – 添加已经注册了的分类法
has_archive- 文章是否有归档,就是一个所有文章归档页面
rewrite – 触发此帖子类型的重写操作。为了防止重写,设置为false。默认值:true,并使用$ post_type作为slug
* $ args数组

* ‘slug’=> string自定义永久链接结构块。默认为$ post_type值。应该是可翻译的
* ‘with_front’=> bool应该使用前置基座添加永久链接结构。(例如:如果你的永久链接结构是/ blog /,那么你的链接将是:false – > / news /,true – > / blog / news /)。默认为true
* ‘feed’=> bool应该为此帖子类型构建一个feed permalink结构。默认为has_archive值。
* ‘pages’=> bool应该是永久链接结构提供分页。默认为true
query_var – 设置此帖子类型的query_var键。 默认值:true – 设置为$ post_type false则表示禁用
can_export – 可以导出此post_type。默认值:true


实例

  1. <?php
  2. add_action('init', 'my_custom_product');
  3. function my_custom_product()
  4. {
  5.     $labels = array(
  6.         'name' => 'Products Name',
  7.         'singular_name' => 'Product Singular Name',
  8.         'add_new' => '添加产品',
  9.         'add_new_item' => '添加产品',
  10.         'edit_item' => '编辑产品',
  11.         'new_item' => '新产品',
  12.         'all_items' => __('所有产品'),
  13.         'view_item' => '查看产品',
  14.         'search_items' => '搜索产品',
  15.         'not_found' =>  '没有找到有关产品',
  16.         'not_found_in_trash' => '回收站里面没有相关产品',
  17.         'parent_item_colon' => '',
  18.         'menu_name' => '产品'

  19.     );
  20.     $args = array(
  21.         'labels' => $labels,
  22.         'description'=> '自定义的产品类型',
  23.         'public' => true,
  24.         'publicly_queryable' => true,
  25.         'show_ui' => true,
  26.         'show_in_menu' => true,
  27.         'query_var' => true,
  28.         'rewrite' => true,
  29.         'capability_type' => 'post',
  30.         'has_archive' => true,
  31.         'hierarchical' => false,
  32.         'menu_position' => 5,
  33.         'menu_icon' => 'dashicons-cart',
  34.         'taxonomies'=> array('post_tag'),
  35.         'supports' => array('title','editor','author','thumbnail','excerpt','comments')
  36.     );
  37.     register_post_type('product',$args);

  38.     $labels = array(
  39.         'name' => '产品分类',
  40.         'singular_name' => '产品分类',
  41.         'search_items' =>  '搜索产品' ,
  42.         'all_items' => '所有产品' ,
  43.         'parent_item' => null,
  44.         'parent_item_colon' => null,
  45.         'edit_item' => '编辑产品' ,
  46.         'update_item' => '更新产品' ,
  47.         'add_new_item' => '添加产品' ,
  48.         'new_item_name' => '新产品',
  49.         'separate_items_with_commas' => '按逗号分开' ,
  50.         'add_or_remove_items' => '添加或删除',
  51.         'choose_from_most_used' => '从经常使用的类型中选择',
  52.         'menu_name' => '分类目录',
  53.     );

  54.     register_taxonomy(
  55.         'products',
  56.         array('product'),
  57.         array(
  58.             'hierarchical' => true,
  59.             'labels' => $labels,
  60.             'show_ui' => true,
  61.             'query_var' => true,
  62.         )
  63.     );
  64. }
  65. ?>
复制代码

这里为了直观方便,我直接使用了中文,更好的应该是使用英文然后通过本地化函数来翻译成中文。通过以上代码我们就可以创建一个名为产品的分类



这种方法前端调用的话只需要创建:archive-product.php 模板即可,这个模板用于分类列表页的模板调用,product是我们的post type的名字。创建 single-product.php 模板即可实现分类的文章的详情页的调用。

‘taxonomies’=> array(‘post_tag’)这段代码意思是给自定义post type添加标签页面,如果需要使用WordPress自带的分类标签方法,可以写为注意,’taxonomies’=> array(‘post_tag’, ‘category’)。

在wordpress中也有一种独立的分类法,与文章类型一样,可以使用register_taxonomy()函数来注册分类方法。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|5ucms.com

GMT+8, 2025-8-13 20:46 , Processed in 0.046875 second(s), 29 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表