Do you want to publish the custom post type to the site? We covered you up. This guide will show you how to programmatically create a custom post type for word press with a small amount of code. Word press was originally created to post. But a few years later, WordPress is not just a platform for posting. Today, it is the most perfect web solution, accounting for almost 40% of all websites. Although many functions are integrated, publishing is still one of the best ways to drag users into the site and continue to participate. WordPress provides the default post type, but if you want to customize the website and make it different, customizing the post type may be a good solution.
This tutorial demonstrates how to programmatically create and publish custom post types for word press. Before we start, let’s have a clearer understanding of what the custom post type is and when it should be used. What is a custom post type? The custom post type is the content type. By default, WordPress has multiple post types. Posts and pages are the most famous, but there are others. Post page attachment navigation menu revision custom CSS change set most users use the first five types, but there are other types. In fact, you may not know that they are of type custom post and are using some of them.
The best example of a custom post type is the woocommerce product. When woocommerce is enabled for the first time, the plug-in will generate a custom post type \
Generally, the custom post type is a new type of post, which users can create according to specific requirements to customize the website. Why and when should I use custom post types? The user-defined article type (CPT) may vary depending on how it is built, so you can use it to get all types of solutions. You can use a custom post type when: You will publish certain types of information on a regular basis, but you cannot publish with the default publishing type. Certain types of posts must be distinguished from standard posts, and some types of post behavior must be changed. If you want to customize the publishing function, publishing process and elements, or apply some special functions, you usually need different types of posts with functions or custom behavior, you must use CPT on the WordPress site.
The custom article type is different from the default article type, so it needs special treatment instead of overriding the function of the default article type. This is a huge advantage because you can create custom article types that fully meet your requirements from scratch, so you need a variety of article types. Now that we have a better understanding of what they are and when they should be used, let’s take a look at how to create custom post types. This section describes how to programmatically create a custom post type for word press. This section provides an example of creating a custom post type in word press. Back up your site before you start, and if not, create a subtopic.
From the panel, navigate to appearance > Theme Editor, in the right column
The functions of the subtheme. Open the PHP file and paste the following script: Register a custom post type named movie. Function quadlayers_custom_post_type (){
\/\/Set UI tags for custom post types
$labels = array(
\
\
\
\
\
\
\
\
\
\
\
\
\
);
\/\/Set additional options for custom post type
$args = array(
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
);
\/\/Register custom post types
Register_post_type (\
}
Add_action (\
After opening it, you can see that the post editor is the same as the default post type, because it sets the same parameters for the post editor screen. So, how do you know this is a custom post type editor? Because you can see the movie post type in the URL and title of the page. Now that you see the final result, you can analyze the code and better understand the operation performed by each part. You just saw how to programmatically create WordPress custom post types through code instructions. But that’s only half what you need to know. Customize the code to create your own post type
must. To do this, you must understand what each part of snipt does.
Use wp_init() hook to make the function run only once. When WordPress starts. The quadlayers_custom_post_type() function has three parts. 1) Tag this is the string set to be used by management pages and front-end pages with different topics. This is set on a single array$ labels = array( ‘name’ => _x( ‘Movies’, ‘Post Type General Name’, ‘storefront’ ), ); 2) Options are predefined options that define the behavior and other features of custom post types (movies).
Each option is a specific description that defines how the custom post type works. \
3) Custom post type registration the word press function register_post_type() will eventually be set to prepare to register and use the new custom post type. Now that you have a better understanding of the code, you can start publishing custom post types. But unlike publishing them, you may want to display your CPT somewhere. Let’s see how to do it. How WordPress displays custom post types so far, we have learned how WordPress creates custom post types programmatically. After publishing, the location of the display must be determined.
There are several ways to print CPT on the front end. You can display a complete list of specific custom post types in other posts, pages, sidebars, or anywhere else. The following sample script will generate a custom shortcut code that shows all custom post types created using the previous function. The following script generates a shortcut code named movies, which prints all the movies added to CPT on all the pages where we added the shortcut code. Add_shortcode (\
Function quadlayers_display_cpt_shortcode (){
$args = array (\
$the_query = new wp_query ($args);
if ( $the_query->have_posts() ) :
Period ($the_query – > have_posts(): $the_query – > the_post();
$c= the_title( sprintf( ‘
‘, esc_url( get_the_title() ) ), ‘
‘).
‘
‘.
the_content().
‘