Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 186|回复: 0

How to Create a Custom Post Type

[复制链接]

2

主题

2

帖子

8

积分

新手上路

Rank: 1

积分
8
发表于 2024-1-31 18:29:27 | 显示全部楼层 |阅读模式

Adding custom post types in WordPress is extremely easy since WordPress includes the core function register_post_type that can be used to create them. This means if you are a plugin developer you can easily include custom post types in the theme you are creating. Or you can add them via your child theme or via a custom plugin.

Creating A Custom Post Type Manually (using code)
First things first, where should you add your code? The best place to register and add your custom post types depends on your project. If you are working on a client site that already has a theme active you’ll want to create a child theme and register your post types from there. If you are creating your Country Email List own custom theme you can place the code in the functions.php file or in any other file called from your functions.php. And if you are developing a plugin it doesn’t really matter where you add the code, so long as the code runs before the ‘init’ action hook to make sure it’s available.



For testing purposes, your functions.php file will do just fine. But a plugin will ensure you won’t break your site on changing or upgrading your theme.

If the custom post type is really important, consider making it a must-use plugin. For the uninitiated, must-use plugins are installed in a special directory inside the content folder and are automatically enabled on all sites. Must-use plugins don’t show in the default list of plugins on the plugins page of wp-admin.

Anyway, a custom post type can be added to WordPress via the register_post_type( ) function. This allows you to define a new one by several labels. Once you’ve created your header, you can use this function before the admin_menu, but after the after_setup_theme action hooks. If created correctly, you can pull this off with only a few lines of code. From the WordPress Codex, here’s a simple example of a new custom post type.

回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|DiscuzX

GMT+8, 2025-9-15 05:51 , Processed in 0.120069 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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