-
Notifications
You must be signed in to change notification settings - Fork 2
Post Types
Nemesis provides helper functions for registering a custom post type and custom taxonomies - these helper functions mean you only have to pass in the bare bones info you need to change from default to get it to work.
The example below will demonstrate how to register Gutenberg templating based on Post Type within your class.
A block template is defined as a list of block items. Such blocks can have predefined attributes, placeholder content, and be static or dynamic. Block templates allow to specify a default initial state for an editor session
Taken from: https://wordpress.org/gutenberg/handbook/designers-developers/developers/block-api/block-templates/
Here we are just extending the base class from Nemesis to gain access to the helper functions.
<?php
namespace SiteName\Classes\PostTypes;
use NanoSoup\Nemesis\PostTypes\PostTypes;
/**
* Class Careers
* @package SiteName\PostTypes
*/
class Careers extends PostTypes
{
Within the constructor we are just registering actions to all our methods to register both the post type and it's template.
/**
* Careers constructor.
*/
public function __construct()
{
add_action('after_setup_theme', [$this, 'register']);
add_action('after_setup_theme', [$this, 'registerTemplate'] );
}
When registering a post type you are only asked to provide the name, slug, singular and multiple labelling for the post type, additionally you can provide an array of attributes which will override the defaults.
Below we are using the attributes array to remove the archive page for this post type and also provide the dashicon for the menu in the CMS.
/**
* Register the custom post type for CaseStudies
*
*/
public function register()
{
$this->registerPostType('careers', 'Job Post', 'Careers', 'careers', [
'has_archive' => false,
'menu_icon' => 'dashicons-welcome-learn-more'
]);
}
When registering the custom taxonomy again you are only asked to provide the name of the post type it is linked to, singular name and multiple name. Again you can provide a 4th argument of an array of attributes to override defaults if needed.
/**
* Register the custom post type for CaseStudies
*
*/
public function register()
{
$this->registerPostType('case-studies', 'Case Study', 'Case Studies', 'case-studies',
[
'menu_icon' => 'dashicons-welcome-widgets-menus'
]);
$this->registerCustomTaxonomy('case-studies', 'Case Study', 'case-studies');
}
When registering a template you need to know the block names that you want to allow.
Once you have this you are then able to amend the post type object by adding the template property to it with your desired structure / placeholders.
/**
*
*/
public function registerTemplate() {
$post_type_object = get_post_type_object( 'careers' );
$post_type_object->template = [
['acf/contentwithoffsetimage'],
['acf/skillsandexperience'],
['acf/perks'],
['acf/imagecollage'],
];
}
When adding the template you are also able to amend the editing capabilities of the user for this template by doing something like: $post_type_object->template_lock = 'all';
For this there are two options:
all — prevents all operations. It is not possible to insert new blocks, move existing blocks, or delete blocks.
insert — prevents inserting or removing blocks, but allows moving existing blocks.