string(11) "newsflashes" string(8) "document" string(4) "shop" string(6) "circle"
  • 首页
  • 动态
  • 商铺
  • 星球学习
  • 头条
  • 文档帮助
  • 会员赞助
  • 问答提问
文章
文章用户商铺文档快讯星球网址导航

{{userData.name}}已认证

文章

评论

关注

粉丝

¥{{role.user_data.money}}
{{role.user_data.credit}}
您已完成今天任务的
  • 私信列表所有往来私信

  • 财富管理余额、积分管理

  • 任务中心每日任务

    NEW
  • 成为会员购买付费会员

  • 认证服务申请认证

    NEW
  • 小黑屋关进小黑屋的人

    NEW
  • 我的订单查看我的订单

  • 我的设置编辑个人资料

  • 进入后台管理

  • 网站首页
  • 网站SEO
  • 站长平台
  • 智慧办公
  • 设计素材
  • 运营管理
  • 商城区
    • 本站服务
投稿
  • 阿里云新客
    领取限量2000元代金券

  • 阿里云博客专属
    博客用户专属优惠

  • 一键采购
    购物车采购可减5000元

WordPress主题添加自定义文章类型register_post_type和分类

  • 网站教程
  • 21年9月2日
  • 编辑
嘀嗒兔嘀嗒兔管理员
释放双眼,带上耳机,听听看~!

 

WordPress功能强大之处其中之一就是支持自定义文章类型和分类,非常的好用。本文给大家简单说一下如何在我们的主题中添加自定义文章类型register_post_type和分类register_taxonomy。

首先,添加自定义文章类型:

/* Register Custom Post Type */
add_action( 'init', 'create_products_post_type' );
// add portfolio
function create_products_post_type() {
	$labels = array(
		'name'               => __('产品', 'WPGP'),
		'singular_name'      => __('产品', 'WPGP'),
		'add_new'            => __('添加', 'WPGP'),
		'add_new_item'       => __('新增产品', 'WPGP'),
		'edit_item'          => __('编辑产品', 'WPGP'),
		'new-item'           => __('新增产品', 'WPGP'),
		'view_item'          => __('查看产品', 'WPGP'),
		'search_items'       => __('搜索产品', 'WPGP'),
		'not_found'          => __('未找到产品', 'WPGP'),
		'not_found_in_trash' => __('垃圾箱未找到产品', 'WPGP'),
		'parent_item_colon'  => '',
	);

	$args = array(
		'labels'             => $labels,
		'show_ui'            => true,  // Whether to generate a default UI for managing this post type in the admin
		'query_var'          => true,
		'show_in_nav_menus'  => false,
		'public'             => true,  // Controls how the type is visible to authors and readers
		'capability_type'    => 'post',
		'hierarchical'       => false,
		'menu_icon'          => 'dashicons-format-gallery', // use a font icon, e.g. 'dashicons-chart-pie'
		'has_archive'        => true,  // Enables post type archives
		'rewrite'            => array( 'slug' => 'products' ),
		'supports'           => array( 'title', 'editor',  'thumbnail', 'excerpt', 'comments', 'custom-fields', 'page-attributes' ),
		'can_export'         => true,
	);

	register_post_type( 'products', $args );
}

接下来我们需要给自定义文章类型添加分类功能,来对自定义文章进行分类管理:

add_action( 'init', 'register_products_taxonomy');

// create two taxonomies, genres and writers for the post type "book"
function register_products_taxonomy() {
	// Add new taxonomy, make it hierarchical (like categories)
	$labels = array(
		'name'              => __('产品分类', 'WPGP'),
		'singular_name'     => __('产品分类', 'WPGP'),
		'menu_name'         => __('产品分类', 'WPGP'),
		'search_items'      => __('搜索', 'WPGP'),
		'all_items'         => __('所有产品分类', 'WPGP'),

		'edit_item'         => __('编辑产品分类', 'WPGP'),
		'update_item'       => __('更新产品分类', 'WPGP'),
		'add_new_item'      => __('添加新的产品分类', 'WPGP'),
		'new_item_name'     => __('新的产品分类', 'WPGP'),
	);

	$args = array(
		'hierarchical'      => true,
		'labels'            => $labels,
		'show_ui'           => true,
		'show_in_menu'      => true,
		'show_in_nav_menus' => true,   
		'query_var'         => true,
		'has_archive'       => false,
		'rewrite'           => array( 'slug' => 'product' ),
	);

	register_taxonomy( 'product', 'products', $args );
}

后,添加一个控制WP后台自定义文章页文章排序的小功能:

// admin page products orderby
add_filter( 'parse_query', 'sort_products_by_date' );
function sort_products_by_date() {
	global $pagenow;

	if ( is_admin() && $pagenow =='edit.php' &&  !empty($_GET['post_type'] == 'products') && !isset($_GET['post_status']) && !isset($_GET['orderby']) ) {
		wp_redirect( admin_url('edit.php?post_type=products&orderby=date&order=desc') );
		exit;
	}
}

给TA打赏
共{{data.count}}人
人已打赏
网站教程

WordPress文章点赞量排行文章

2021-1-12 16:32:06

网站教程

7b2主题实现用户发布或更新文章后,通知粉丝

2024-12-14 23:42:36

也想出现在这里?联系我们吧
嘀嗒兔
1 条回复 A文章作者 M管理员
  1. 嘀嗒兔
    嘀嗒兔AM嘀嗒兔管理员 学前班lv1
    21年9月19日

问答

PREVNEXT
热门 最新 等待回答
没有内容
  • 个回答
    {{item.metas.reward.money}} ¥{{item.metas.reward.money}}

文章聚合

  • TOP1
    Microsoft Teams 登陆时提示“有人已经为你的组织设置了

    Microsoft Teams 登陆时提示“有人已经为你的组织设置了

    20年8月5日
  • TOP2
    [仅限会员]WordPress用户评论/回复/购买/订单等功能的管理员消息提醒-插件版

    [仅限会员]WordPress用户评论/回复/购买/订单等功能的管理员消息提醒-插件版

    22年3月4日
  • TOP3
    [仅限会员]电脑端实时动态弹幕,7B2,日主题,zibi主题,总裁主题,Verdure,非狗叫版本以及接口

    [仅限会员]电脑端实时动态弹幕,7B2,日主题,zibi主题,总裁主题,Verdure,非狗叫版本以及接口

    22年3月4日
  • (上传中)抖音利世3.45G 全网最牛利世套图+视频打包

    (上传中)抖音利世3.45G 全网最牛利世套图+视频打包

    22年3月1日
  • (已补链接)@抖娘-利世 写真套图合集50套 17.49G

    (已补链接)@抖娘-利世 写真套图合集50套 17.49G

    22年3月2日
  • 如何购买域名?

    如何购买域名?

    20年8月3日
❯

解锁会员权限

开通会员

解锁海量优质VIP资源

立刻开通

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索
  • 扫码打开当前页

返回顶部
幸运之星正在降临...
点击领取今天的签到奖励!
恭喜!您今天获得了{{mission.data.mission.credit}}积分

今日签到

连续签到

  • {{item.credit}}
  • 连续{{item.count}}天
查看所有
我的优惠劵
  • ¥优惠劵
    使用时效:无法使用
    使用时效:

    之前

    使用时效:永久有效
    优惠劵ID:
    ×
    限制以下商品使用: 限制以下商品分类使用: 不限制使用:
    [{{ct.name}}]
    所有商品和商品类型均可使用
没有优惠劵可用!

购物车
  • ×
    删除
购物车空空如也!

清空购物车 前往结算
您有新的私信
没有新私信
写新私信 查看全部

关于我们

  • 关于我们

    了解我们做站的初衷

  • 免责声明

    浏览前仔细阅读本站声明

  • 用户协议

    注册前仔细阅读本站协议

  • 提交建议

    在线提交问题建议

售后咨询

  • 优惠活动

    查看本站的最新优惠

  • 配件商城

    在线购买XX配件

  • 法律声明

    本站的法律声明

  • 在线工单

    提交在线工单

联系与合作

  • 申请友链

    网站IT互联网相关链接交换

  • 广告合作

    广告服务欢迎投放共创双赢

  • 文章创作

    提供优秀的电影创作与分享

  • 推广中心

    推广分享文章链接获取收益

关注我们

嘀嗒兔 一个神奇的互联网+

Copyright © 2025 嘀嗒兔
・陕ICP备2024055903号
查询 7 次,耗时 0.1423 秒
首页专题认证
搜索菜单我的