In our previous discussion, we already made clear conception of meta box and also learned how to add custom text field to any page/post type.
Today we will discuss same thing but instead of text field we will show how to add radio button for any post type. The all functional process are same but need to changes the HTML format & added function for update the meta box.
Again we check the function which will work for us to create a meta box in admin panel.
add_meta_box( $id, $title, $callback, $post_type, $context,$priority, $callback_args );
So now we add this function to admin for administrative interface. Let say we would like to get user input where user will select for whom the post is going to be published. To be developed we can create three input field with radio button.
So here is the example:
// ==Add Meta Box “Who will benefited from this post” with Post type=== add_action( 'admin_init', 'add_radio_button_field'); function add_radio_button_field() { add_meta_box('radio_button_fields_id', 'Select Beneficiary', 'post_beneficiary_fields', 'post', 'normal', ‘high’ ); } function post_beneficiary_fields(){ global $post; $all_beneficiary_fields= get_post_custom($post->ID); $value=$all_beneficiary_fields["post_benefit"][0]; ?> <label>Who will get benefit from this post ? </label> <input type="radio"<?php checked($value,'non-dev');?>name="post_benefit" value="non-dev"/> Non-Developers <input type="radio" <?php checked($value,'dev');?> name="post_benefit" value="dev" /> Developers <input type="radio" <?php checked( $value,'both');?> name="post_benefit" value="both"/> Both <?php } add_action('save_post', 'save_post_beneficiary'); function save_link(){ global $post; update_post_meta($post->ID, "post_benefit", $_POST["post_benefit"]); }
That’s all. If you refresh your post type page, you can see this mata box added under the rich area. Now question how to get this value in template. There are two functions for this:
Function 1: $meta_values = get_post_meta( $post_id, $key, $single );
Function 2: $custom_fields = get_post_custom($post->ID);
So if use first function, we can call it from inside the loop :
$get_beneficiary = get_post_meta( $post->ID, 'post_benefit', true); echo $get_beneficiary;
But if we use second function, we have to get all meta values for this post id 1st then get the specific meta values:
$all_beneficiary_fields= get_post_custom($post->ID); $get_beneficiary=$all_beneficiary_fields["post_benefit"][0]; echo $get_beneficiary;
So that’s all. Hope there is no confusion to get this meta value through meta boxes.Still if you have any further question feel free to drop a message here. We will response in our shortest period.
Thanks.
This post has already been read 7973 times!
What is Lead Capture: In general sense lead capture designates a process or way of…
Introduction Unlimited Modal POPUPS on MouseClick: Everyone knows about popup during site visits and most…
Brief about Pregnancy Calculator : This WordPress Pregnancy Calculator plugin allows users to display an…
Introduction About this Service Plugin: This Visual Composer Service Plugin works as an addon to…
Introduction of Dynamic Query String : This plugin allows user to set dynamic query string…
There is an occasional bug for iPhone and iPad even they are designed as well and…