Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set the Post Type custom article type in WordPress

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to set the Post Type custom article type in WordPress. The article is very detailed and has a certain reference value. Interested friends must finish reading it!

What is a custom post?

Don't take it for granted that the post here refers to the article in the blog, it is just a proxy word for the article category, and you can even think of it as content.

There is no standard specification for the custom model. The article model can be any content model you want. For example, wordpress itself has the following content article models built into it:

Blog article

Page

Attachment

Correction

Navigation, etc.

You can understand it this way: as long as it is a flexible form of content that we use to create, edit, and store data as we use blog posts.

But here I still need to remind you, the blog built-in post is still a little different, you can use it to contain categories, tags and so on to identify the content!

Why customize the article model?

Wordpress already provides some perfect default article models, which are suitable for most sites, but we still need more choices. I listed some of the content models that I thought might be useful and linked to corresponding examples.

Real estate list

Event calendar (I know a lot of people are interested in this)

Film and television database

Book database

Forum system without many integration problems

Ticketing system similar to WordPress Trac

Design a photo album or album

You can also think of more content models than I listed. And I also want to learn more about forums and ticketing systems in the future. I have implemented these two systems and hope to get some feedback.

Create a post type

To create a new Post Type, you need to register using the register_post_type function. You need to call this function in the functions.php file of your theme:

Register_post_type ($post_type, $args)

The $post_type parameter is the name of your custom Post Type. Post Type can customize a lot of functions, so there will be a lot of $args arguments in this function. So it is usually registered in the following format:

Function my_custom_post_product () {$args = array (); register_post_type ('product', $args);} add_action (' init', 'my_custom_post_product')

Wrap it in a function, define an array, and attach it to the action of init. In this way, when WordPress initializes, it executes this function to register a custom Post Type, because when calling register_post_type (), it must be before admin_menu action and after after_setup_theme action, so it is best to attach it to init action.

There are many parameters. For the convenience of writing tutorials, only the more commonly used parameters are listed. The general structure is as follows:

Function my_custom_post_movie () {$labels = array ('name' = > _ x (' Movies', 'post type name'), 'singular_name' = > _ x (name when' Movie', 'post type is a single item) Because there is plural in English, 'add_new' = > _ x (' New Movie', 'Link name to add New content'), 'add_new_item' = > _ (' New Movie'), 'edit_item' = > _ (' Edit Movie'), 'new_item' = > _ (' New Movie') 'all_items' = > _ (' all movies'), 'view_item' = > _ (' View movies'), 'search_items' = > _ (' search movies'), 'not_found' = > _ _ (' no movies found'), 'not_found_in_trash' = > _ _ (there are no related movies in the Recycle Bin') 'parent_item_colon' = >', 'menu_name' = >' Movies') $args = array ('labels' = > $labels,' description' = > 'movie information on our website', 'public' = > true,' menu_position' = > 5, 'supports' = > array (' title', 'editor',' thumbnail', 'excerpt',' comments'), 'has_archive' = > true); register_post_type (' movie', $args);} add_action ('init',' my_custom_post_movie')

Here, for the sake of intuition and convenience, I directly use Chinese, it is better to use English and then translate it into Chinese through localization functions.

There are a lot of parameters, you can also use the generatewp tool to customize the parameters, and then change them, which will be a little more convenient.

From the above code, you can see that there is a labels configuration item in the $args array, which is used to configure and display copy-related content, so it is taken out separately to create an array for clarity. You can also guess the general meaning of other configuration items in English. If you want to know more, you can take a look at the official document: register_post_type.

Add the above code to the bottom of the theme functions.php, and when you enter the background, you will find that there is an extra Movies option, which indicates that the registration is successful:

At this point, we can create a new Movie to publish a movie-style article. But this is basically the same as the article type, we need more customization to improve our Movie type.

Add classification function to Post Type

As far as movies are concerned, they can be divided into science fiction, action, war and other categories, so we can add classification features to the custom Movie so that we can edit new categories and classify our movies. This classification is the same as the classification in the article.

To add the classification function, you need to use the function register_taxonomy, which is also easy to use, similar to registering the Post Type function, except that one more parameter is used to specify the corresponding Post Type:

Register_taxonomy ($taxonomy, $object_type, $args)

For this example, you can configure the following common parameters:

Function my_taxonomies_movie () {$labels = array ('name' = > _ x (' movie category', 'taxonomy name'), 'singular_name' = > _ x (' movie category', 'taxonomy singular name'), 'search_items' = > _ (' search movie category'), 'all_items' = > _ (' all movie categories') 'parent_item' = > _ (' parent category of this movie category'), 'parent_item_colon' = > _ (' parent category of this movie category:'), 'edit_item' = > _ (' edit movie category'), 'update_item' = > _ (' update movie category'), 'add_new_item' = > _ (' add new movie category') 'new_item_name' = > _ _ (New Film Category'), 'menu_name' = > _ (' Film Category'),) $args = array ('labels' = > $labels,' hierarchical' = > true,); register_taxonomy ('movie_category',' movie', $args);} add_action ('init',' my_taxonomies_movie', 0)

After adding it to the topic, we see the familiar article classification function, but all the copy above has become our custom content:

Here we add two categories as a demonstration.

Add a custom Meta Box for Post Type

The type of movie we want to add is not only the content of the text, we also need to add some additional content such as the director. Then you need to add a custom Meta Box,Meta Box. You can add a custom form to the post page, fill in additional information when writing the article, and then call it at the front end.

The add_meta_box function is required for customizing Meta Box:

Add_meta_box ($id, $title, $callback, $post_type, $context,$priority, $callback_args)

According to the old rules, please see the official documentation for specific parameters. Only common usage is introduced here. We sign up for a Meta Box:

Add_action ('add_meta_boxes',' movie_director'); function movie_director () {add_meta_box ('movie_director',' film director', 'movie_director_meta_box',' movie', 'side',' low');}

Then we specify the callback function movie_director_meta_box in the configuration parameters. We need to create a form in this function:

Function movie_director_meta_box ($post) {/ / create a temporary hidden form for security wp_nonce_field ('movie_director_meta_box',' movie_director_meta_box_nonce'); / / get the previously stored value $value = get_post_meta ($post- > ID,'_ movie_director', true);? >

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report