Skip to content

Custom Post Type

Loïc Antignac edited this page Aug 5, 2022 · 11 revisions

Once the the plugin is created, the declaration(s) can be added:

<?php
/**
 * Plugin Name:       Example Custom Post Type
 * Author:            My Name
 * Text Domain:       wax-custom-content
 * Domain Path:       /languages
 */
defined( 'ABSPATH' ) || exit;

// If you already have an autoload (for example if you are on Bedrock) you can remove these 3 lines, otherwise leave them.
if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
	require __DIR__ . '/vendor/autoload.php';
}

use Webaxones\Core\Entities\Entities;

Webaxones\Core\Library::init( 'webaxones-content' );

/**
 * Custom Post Type: Project
 */
$declarations[] = [
	'entity'   => 'Webaxones\Core\Classification\PostType',
	'labels'   => [
		'gender'            => 'm',
		'name'              => _x( 'Projects', 'Capitalized Plural Name', 'webaxones-content' ),
		'singular_name'     => _x( 'Project', 'Capitalized Singular Name', 'webaxones-content' ),
		'parent_item_colon' => __( 'Parent project: ', 'webaxones-content' ),
		'all_items'         => __( 'All projects', 'webaxones-content' ),
		'new_item'          => __( 'New project', 'webaxones-content' ),
		'the_singular'      => __( 'The project', 'webaxones-content' ),
		'the_plural'        => __( 'The projects', 'webaxones-content' ),
	],
	'settings' => [
		'slug'          => 'project',
		'taxonomies'    => [],
		'supports'      => [ 'title', 'thumbnail', 'editor', /*'excerpt', 'author', 'comments', 'revisions', 'page-attributes', 'post-formats', 'custom-fields', 'trackbacks'*/ ],
		'menu_icon'     => 'dashicons-portfolio',
		'menu_position' => 7,
		'hierarchical'  => false,
		'public'        => true,
		'show_in_rest'  => true,
		'has_archive'   => true,
		'rewrite'       => [
			'slug'       => 'business-project',
			'with_front' => true,
			'pages'      => true,
			'feeds'      => true,
		],
	],
];

Entities::process( $declarations );

Labels

PostType entity has a gender key in the labels. The value is m (for masculine) by default.
All the labels can be translated from the 7 values presented in labels above.

Settings

As for all entities, settings starts with the slug key which identifies the entity and must be unique.
All arguments accepted by the register_post_type function can be added in settings.

Clone this wiki locally