Create a custom post type
With an other tiny example :)
1. Make the Tea Theme Options working
As we said before:
<?php
use crewstyle\TeaThemeOptions\TeaThemeOptions;
//Include composer autoload
require_once __DIR__.'/vendor/autoload.php';
//Instanciate a new TeaThemeOptions
$tea = new TeaThemeOptions();
2. Define what you want
Specify your wanted components:
<?php
//Build post type
$tea_configs = array(
'slug' => 'project',
'labels' => array(
'name' => __('Projects'),
'singular_name' => __('Project'),
),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_icon' => 'dashicons-portfolio',
'menu_position' => 5,
'public' => true,
'query_var' => true,
'show_ui' => true,
'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'revision', 'comments'),
'taxonomies' => array(),
);
//Build contents
$tea_contents = array(
array(
'type' => 'textarea',
'id' => 'source',
'title' => 'Video URL',
'placeholder' => 'http://...',
'description' => 'Enter here your Video URL.'
)
);
//Add post type to WP
$tea->addPostType($tea_configs, $tea_contents);
//Delete variables because we love our server!
unset($tea_configs, $tea_contents);
If you need to upgrade post
or page
post types, you can do it in the backend panel only:
<?php
//Check if we are in the admin panel
if (is_admin()) {
//Build post type
$tea_configs = array(
'title' => 'post',
'slug' => 'post' //or 'page'
);
//Build contents
$tea_contents = array(
array(
'type' => 'select',
'id' => 'select_box_id',
'title' => 'A selectbox',
'default' => 'great',
'options' => array(
'lovethat' => 'I love that!',
'great' => 'GREAT',
'toobad' => 'Too baaaaaaaaaaad!',
)
)
);
//Add post type to WP
$tea->addPostType($tea_configs, $tea_contents);
//Delete variables because we love our server!
unset($tea_configs, $tea_contents);
}
3. Build the CPT
<?php
//Build post types
$tea->buildPostTypes();
4. All in one
Here is a complete snapshot of what you have to do. Remember to make all these modifications in your functions.php
theme file.
<?php
use crewstyle\TeaThemeOptions\TeaThemeOptions;
//Include composer autoload
require_once __DIR__.'/vendor/autoload.php';
//Instanciate a new TeaThemeOptions
$tea = new TeaThemeOptions();
//Build post type
$tea_configs = array(
'slug' => 'project',
'labels' => array(
'name' => __('Projects'),
'singular_name' => __('Project'),
),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_icon' => 'dashicons-portfolio',
'menu_position' => 5,
'public' => true,
'query_var' => true,
'show_ui' => true,
'supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'revision', 'comments'),
'taxonomies' => array(),
);
//Build contents
$tea_contents = array(
array(
'type' => 'textarea',
'id' => 'source',
'title' => 'Video URL',
'placeholder' => 'http://...',
'description' => 'Enter here your Video URL.'
)
);
//Add post type to WP
$tea->addPostType($tea_configs, $tea_contents);
//Delete variables because we love our server!
unset($tea_configs, $tea_contents);
//Check if we are in the admin panel
if (is_admin()) {
//Build post type
$tea_configs = array(
'title' => 'post',
'slug' => 'post' //or 'page'
);
//Build contents
$tea_contents = array(
array(
'type' => 'select',
'id' => 'select_box_id',
'title' => 'A selectbox',
'default' => 'great',
'options' => array(
'lovethat' => 'I love that!',
'great' => 'GREAT',
'toobad' => 'Too baaaaaaaaaaad!',
)
)
);
//Add post type to WP
$tea->addPostType($tea_configs, $tea_contents);
//Delete variables because we love our server!
unset($tea_configs, $tea_contents);
}
//Build post types
$tea->buildPostTypes();
Pay attention: do not hesitate to make conditions on what you need in your backend panel versus what you need in your frontend website.
Updated less than a minute ago