Хей, имам нещо специално за теб!
Попълни имейл адрес, на който до минута ще получиш обещаното ;)
By default, when you install WordPress, you get three types of post types: posts, pages, and media. Fortunately, starting from version 2.9, we can create as many custom post types as we want. But what are they used for and how can you apply them in the real world? We’ll explore that shortly!
A custom post type in WordPress is essentially the same as the default post type, with the only difference being its post-type value in the database. For example, the post type “Page” has a post-type value of “page,” the post type “Media” has a post-type value of “attachment,” and the default post type has a post-type value of “post.” In this regard, you can now create custom post types with values such as “movies,” “books,” “episodes,” and so on.
When created and properly configured, a custom post type can help you achieve the following with just a few lines of code:
In addition to all of this, you can specify dozens of other options, such as where the new custom post type should appear in the menu, whether it should be searchable, which user roles have access to it, whether it should have a hierarchical structure, and more.
For effective creation and usage of a custom post type, you should familiarize yourself with the following (don’t worry, we’ll explore each point in detail ?):
These aspects will provide you with a solid foundation to work with custom post types in WordPress.
Like most things in WordPress, here we have a ready-made function called register_post_type();
:
function my_custom_post_product() {
$args = array();
register_post_type( 'portfolio', $args );
}
add_action( 'init', 'my_custom_post_portfolio' );
Created in its simplest form (above), this custom post type has almost no customization. It won’t be public, won’t show up in the admin, the messages during interaction will be the same as default posts, and so on. To add custom settings that meet our needs, we need to add specific arguments and parameters to the $args
array (which is empty in the example above).
function my_custom_post_project() {
$labels = array(
'name' => __('Projects', 'theme'),
'singular_name' => __('Project', 'theme'),
'menu_name' => __('Projects', 'theme'),
'all_items' => __('All Projects', 'theme'),
'view_item' => __('View Project', 'theme'),
'add_new_item' => __('Add New Project', 'theme'),
'add_new' => __('Add New', 'theme'),
'edit_item' => __('Edit Project', 'theme'),
'update_item' => __('Update Project', 'theme'),
'search_items' => __('Search in Projects', 'theme'),
'not_found' => __('Not Found', 'theme'),
'not_found_in_trash' => __('Not Found in Trash', 'theme'),
);
$supports = array(
'title', // post title
'editor', // post content
'author', // post author
'thumbnail', // featured images
'excerpt', // post excerpt
'custom-fields', // custom fields
'comments', // post comments
'revisions', // post revisions
'post-formats', // post formats
);
$args = array(
'label' => __('Project', 'theme'),
'description' => __('All Projects lives here', 'theme'),
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'can_export' => true,
'has_archive' => true,
'publicly_queryable' => true,
'show_in_rest' => true,
'menu_icon' => 'dashicons-image-filter',
'menu_position' => 8,
'supports' => $supports,
);
register_post_type( 'project', $args );
}
add_action( 'init', 'my_custom_post_product' );
$supports
array specifies whether the new custom post type supports basic settings/properties such as comments, title, excerpt, editor, revisions, etc. $labels
array contains all the settings for the new custom post type in the admin area – here you can configure how its name appears in the menu, what system messages to display, and more.$args
array stores information about the slug of the new custom post type, its position in the left menu in the admin area, the icon it should have, and other details.⭐ For an icon to be displayed in the administration menu, you can use dashicons.
Standard posts use categories and tags to create the organizational structure of the website. However, this structure may not be applicable to the newly created custom post type. For example, in standard posts, you may have categories like “News” and “Events,” but these may not be suitable for a CPT (Custom Post Type) like “Projects” where you might want to have categories like “Design,” “Marketing,” and “Brands.”
Fortunately, there is a solution in the form of creating a custom taxonomy. Here is an example of the code for creating a custom taxonomy for the CPT projects:
function registerProjectTypeTaxonomy() {
$labels = [
'name' => __('Project type', 'theme'),
'singular_name' => __('Project type', 'theme'),
'search_items' => __('Search Project types', 'theme'),
'popular_items' => __('Popular Project types', 'theme'),
'all_items' => __('All Project types', 'theme'),
'parent_item' => __('Parent Project types', 'theme'),
'parent_item_colon' => __('Parent Project types', 'theme'),
'edit_item' => __('Edit Project type', 'theme'),
'update_item' => __('Update Project type', 'theme'),
'add_new_item' => __('Add New Project type', 'theme'),
'new_item_name' => __('New Project type', 'theme'),
'add_or_remove_items' => __('Add or remove type', 'theme'),
'choose_from_most_used' => __('Choose from most used project types', 'theme'),
'menu_name' => __('Types', 'theme'),
];
$test = register_taxonomy(
'project-types',
'project',
array(
'labels' => $labels,
'hierarchical' => true,
'query_var' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'show_in_rest' => true,
'has_archive' => false,
'with_front' => false,
'rewrite' => ['slug' => 'types','with_front' => false]
)
);
register_taxonomy_for_object_type('project', 'project-types'); // assign this taxonomy to the CPT you have
}
add_action('init', 'registerProjectTypeTaxonomy');
The important part here is to assign the taxonomy to the specific custom post type it relates to. To do that, you need to include the taxonomies
parameter when registering the custom post type.
If you need a custom post type, most likely your goal is to have a specific layout and functionality for each post added to it. For example, if the post type is “Projects,” you may want to have a gallery with images, a case study section, a testimonial section, and so on. This template page will be completely different from the template for a regular post.
By default, all posts use the single.php
file as the template. To create a new and unique template for your custom post type, go to the main theme directory (root) and create a new file. After “single,” write the slug name of the custom post type. For example, if you have a custom post type called “projects,” your template file should be named single-projects.php
.
By doing this, you are instructing WordPress to use the single-projects.php
template for all posts that belong to the custom post type “projects.”
In the newly created template, you can have a different layout than the default one, different fields, and in general, present specialized information in a unique and distinct way.
With this, the lesson on creating a custom post type and all its specificities concludes. I hope it has been helpful to you.
Stay blond 😉