How to add Meta Box to WordPress
This article will explain in detail how to add Meta Box in WordPress, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
WordPress's method of adding Meta Box requires the use of add meta boxes Action, which allows us to register Meta Box for any article type, in which we need to use the add_meta_box () method to add information about Meta Box.
The code is as follows
Function add_rating_meta_box ($post_type, $post) {/ / which post type is needed to add the recommendation index Meta Box $types = array ('post',' page') Foreach ($types as $type) {add_meta_box ('rating_meta_box_id', / / Meta Box's id in the foreground page). You can get the Meta Box' recommendation index 'through JS, and / / display the title' render_rating_meta_box', / / callback method The HTML code $type to output the Meta Box, / / which post type page to add 'side', / / where to display the Meta Box' default' / / priority) }} add_action ('add_meta_boxes',' add_rating_meta_box')
Here we define the custom field that both Post and Page need the recommendation index in the $types array, and then tell WordPress to use the "render_rating_meta_box" method to render the Meta Box in the sidebar (side). Because there is not much content, so the sidebar is enough, if there is more content, you can change "side" to "advanced", so that Meta Box will be rendered in the main content area.
Next, let's see how it is rendered.
Function render_rating_meta_box ($post) {/ / add a nonce entry for subsequent security checks wp_nonce_field ('rating_nonce_action',' rating_nonce_name'); / / get the value of the recommendation index $rating_key = 'rating'; $rating_value = get_post_meta ($post- > ID, $rating_key, true); $rating_value = (int) $rating_value $html =''; for ($I = 0; $I