Skip to main content
We are Brand SEO Beijing serving international business, your marketing partner, Contact us by

Register a custom post type

Why register a custom post type for wordpress other than the default post?For example, on this site, the "tutorial" is displayed in the form of a general blog post, but there is also a "topic" article, which needs to be displayed in another form, which may be difficult to achieve by ordinary methods, so another Register an article type, wp can use a separate template to display each article type, so that we can easily achieve the effect we want.
However, the actual use of custom article types is not limited to this. For example, I have seen a custom article type that is specially registered just to add setting information such as pictures to a picture switch banner on the theme home page. .
After reading the previous article, the function I want to use to create a custom post type is clear.

For the args array, it is more important and there are many parameters. The details are as follows: /**

label – (string, optional, unimportant) default same as $post_type
labels – (array, optional) Some descriptive text to configure the post type to display in the background.Default is empty.

  • 'name' - the name of the article type, this can be in Chinese (usually plural, for Chinese there is no plural).
  • 'singular_name' - the name of a single article object, (for English, it is the singular of name), the default is the value of name
  • 'add_new' - corresponds to 'write an article' in the default article type
  • 'add_new_item'-
  • 'edit_item' - edit
  • 'new_item'
  • 'view_item'
  • 'search_items'
  • 'not_found'
  • 'not_found_in_trash'
  • 'parent_item_colon'
  • 'menu_name'

description - some short introductory text
public-(boolean), used to define values ​​for publicly_queriable, show_ui, show_in_nav_menus and exclude_from_search
publicly_queryable - (boolean) variable that can be fetched from foreground (from url, like url rewrite)
exclude_from_search – (Boolean), whether it can be searched
show_ui – (Boolean) Whether to generate a default admin page, that is, whether there is an admin page in the background.The default is the same as public
show_in_menu – Whether to display in the background menu item, if true, then the value of show_ui must also be set to true, there will be a top-level menu item.Can also be a string, like 'tools.php' or 'edit.php?post_type=page'
menu_position – the position in the background menu.
menu_icon – the menu icon (a url).
capability_type – the type of capability to view, edit, and delete, the default is post
capabilities – (array, not used by ordinary people)
map_meta_cap – (Boolean), only used if capabilities are set
hierarchical – (Boolean), whether the article has a hierarchical relationship, that is, whether it is allowed to have parent articles.
supports – (array), some functional support for the article type

  • 'title' - title
  • 'editor' (content) – content editor
  • 'author' – the author
  • 'thumbnail' – featured image, the theme has to support featured images
  • 'excerpt' – abstract
  • 'trackbacks'
  • 'custom-fields' - custom fields
  • 'comments' – comments
  • 'revisions' – revisions
  • 'page-attributes' – page attributes, similar to page, select the one for the page template

register_meta_box_cb – function called when remove_meta_box() and add_meta_box() are executed
taxonomies - add registered taxonomies (e.g. default taxonomies, tags)
has_archive – Whether the article has an archive, it is an archive page of all articles.
rewrite – (boolean or array), whether there is url rewriting, if it is set to false, it will prevent url rewriting, and will be explained in detail in later tutorials about rewriting.
query_var – used for url rewriting
can_export – whether to export
show_in_nav_menus – whether to appear in options on the settings menu page
_builtin - wordpress developers recommend that you do not use this parameter.
_edit_link - wordpress developers advise you not to use this parameter

The above parameters are listed in this way. I can't clearly explain what the specific effect of each parameter is, so let's use a simple example to illustrate the actual effect of the above parameters. We still use the default theme of wordpress. To test twentyten, create a new post-type.php file in the twentyten theme folder, and then add the code at the end of the functions.php file:


In post-type.php, we can add the code for registering the article type. Add the following code in post-type.php:

'Book name', 'singular_name' => 'Book singularname', 'add_new' => 'Add_new', 'add_new_item' => 'add_new_item', 'edit_item' => 'edit_item', 'new_item' => 'new_item' , 'view_item' => 'view_item', 'search_items' => 'search_items', 'not_found' => 'not_found', 'not_found_in_trash' => 'not_found_in_trash', 'parent_item_colon' => '', 'menu_name' => 'menu_name' ); $args = array( 'labels' => $labels, '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' => null, 'supports' => array('title' ,'editor','author','thumbnail','excerpt','comments') ); register_post_type('book',$args); } ?>

After adding the above code, enter the background and check the effect.

Back to Top