wordpress文章类型函数:register_post_type()

这个函数主要是用在wordpress后台用与自定义文章类型的,使用后可以在wordpress后台单独增加一个分类,可以在此分类下发表文章。这里指的是添加一个分类,若是要调取分类用这个。

语法

register_post_type($post_type,$args)

参数

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

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

// 注册自定义文章形式
function custom_post_type(){
$labels = array(
'name' => 'Post Type General Name',
'singular_name' => 'Post Type Singular Name',
'menu_name' => 'Post Types',
'name_admin_bar' => 'Post Type',
'archives' => 'Item Archives',
'attributes' => 'Item Attributes',
'parent_item_colon' => 'Parent Item:',
'all_items' => 'All Items',
'add_new_item' => 'Add New Item',
'add_new' => 'Add New',
'new_item' => 'New Item',
'edit_item' => 'Edit Item',
'update_item' => 'Update Item',
'view_item' => 'View Item',
'view_items' => 'View Items',
'search_items' => 'Search Item',
'not_found' => 'Not found',
'not_found_in_trash' => 'Not found in Trash',
'featured_image' => 'Featured Image',
'set_featured_image' => 'Set featured image',
'remove_featured_image' => 'Remove featured image',
'use_featured_image' => 'Use as featured image',
'insert_into_item' => 'Insert into item',
'uploaded_to_this_item' => 'Uploaded to this item',
'items_list' => 'Items list',
'items_list_navigation' => 'Items list navigation',
'filter_items_list' => 'Filter items list'
);
$args = array(
'labels' => $labels,
'description' => 'Post Type Description',
'supports' => array('title','editor','author','thumbnail','excerpt','comments'),
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-cart',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type('post_type', $args);
}
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

实例

<?php
add_action('init', 'my_custom_product');
function my_custom_product()
{
$labels = array(
'name' => 'Products Name',
'singular_name' => 'Product Singular Name',
'add_new' => '添加产品',
'add_new_item' => '添加产品',
'edit_item' => '编辑产品',
'new_item' => '新产品',
'all_items' => __('所有产品'),
'view_item' => '查看产品',
'search_items' => '搜索产品',
'not_found' => '没有找到有关产品',
'not_found_in_trash' => '回收站里面没有相关产品',
'parent_item_colon' => '',
'menu_name' => '产品'
 
);
$args = array(
'labels' => $labels,
'description'=> '自定义的产品类型',
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => 5,
'menu_icon' => 'dashicons-cart',
'taxonomies'=> array('post_tag'),
'supports' => array('title','editor','author','thumbnail','excerpt','comments')
);
register_post_type('product',$args);
 
$labels = array(
'name' => '产品分类',
'singular_name' => '产品分类',
'search_items' => '搜索产品' ,
'all_items' => '所有产品' ,
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => '编辑产品' ,
'update_item' => '更新产品' ,
'add_new_item' => '添加产品' ,
'new_item_name' => '新产品',
'separate_items_with_commas' => '按逗号分开' ,
'add_or_remove_items' => '添加或删除',
'choose_from_most_used' => '从经常使用的类型中选择',
'menu_name' => '分类目录',
);
 
register_taxonomy(
'products',
array('product'),
array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'query_var' => true,
)
);
}
?>

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

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

欢迎关注我的公众号“xx主题网”,原创技术文章第一时间推送。

文章来源:https://www.xxzhuti.com/594.html


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ERR_TOO_MANY_REDIRECTS错误是指在访问WordPress后台时出现的错误提示,表示重定向次数过多。这个错误通常是由于配置问题或者服务器设置引起的。解决这个问题的方法有几种。 一种方法是检查WordPress的设置。确保在WordPress的设置中,网站的URL和站点地址都正确设置为你的域名。还可以尝试禁用所有插件,然后逐个重新启用插件,以确定是哪个插件引起了重定向问题。 另一种方法是检查服务器的设置。首先,确保你的网站的域名已经正确指向服务器,并且服务器的配置文件中没有重定向规则。另外,还可以尝试删除服务器上的缓存文件或者重启服务器来清除任何可能引起重定向问题的缓存。 如果以上方法都没有解决问题,还可以尝试修改WordPress的配置文件。通过FTP或者文件管理器找到WordPress的根目录下的wp-config.php文件,添加以下代码: define('WP_HOME','http://yourdomain.com'); define('WP_SITEURL','http://yourdomain.com'); 将上面的代码中的"yourdomain.com"替换为你的域名。保存文件后,重新访问WordPress后台,看看是否问题已经解决。 总之,ERR_TOO_MANY_REDIRECTS错误可能是由于WordPress的配置问题或者服务器设置引起的。通过检查WordPress的设置、服务器的设置和修改WordPress的配置文件,你应该能够解决这个问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [解决安装wordpress时出现ERR_TOO_MANY_REDIRECTS重定向次数过多问题 原创</font>](https://download.csdn.net/download/weixin_38677648/12900397)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Nginx + WordPress ERR_TOO_MANY_REDIRECTS](https://blog.csdn.net/cyan20115/article/details/106548984)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值