This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Adding a post type
Koen edited this page Sep 12, 2019
·
6 revisions
Post types are the core of the most WordPress websites, think of them as models. We're using a PostTypeAbstract
with sensible defaults, which is extended per post type.
- Create a class in
Grrr\PostTypes
, extendingPostTypeAbstract
- Call the
register
function infunctions.php
An example for a post type class could be:
namespace Grrr\PostTypes;
class Example extends PostTypeAbstract {
protected $_type = 'example';
protected $_slug = 'examples';
protected $_name = 'Examples';
protected $_singular_name = 'Example';
protected $_icon = 'dashicons-portfolio';
protected $_args = [
'public' => true,
'has_archive' => true,
'taxonomies' => [
'example-type',
],
];
}
For all available arguments of the underlying register_post_type
used, view the WordPress codex or code reference.
A few common configuration options for post types are:
-
No featured image: remove the
thumbnail
from thesupports
array and add it to your class. -
Adding a taxonomy: add the taxonomy slug to the
taxonomies
array and add it to your class. -
No single/detail page: set
public
tofalse
. It will not have an archive page, but is queryable. -
No archive/index page: set
has_archive
tofalse
. It will still have a single/detail page, and is queryable.
You can choose an icon from the WordPress Dashicons, which will be visible in the CMS for the newly created post type.
- The registered name cannot be longer than 20 characters