Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

echo from Radio Button & Checkbox from Custom Meta Box WordPress


I have set up a Custom Meta Box that works well with a Radio Button and Checkbox

I am after the <strong>full code</strong> to echo the values of the Radio Button and Checkbox.

Step 1: echo the values of a Radio Button:
<label for="star_rating">' . __("Star Rating", 'myplugin_textdomain' ) . '</label>';

echo '<input type="radio" name="star_rating" value="none"';
if($star_rating == 'none'){echo 'checked';}else{echo '';};
echo '/> Not Star Rated';

echo '<input type="radio" name="star_rating" value="half"';
if($star_rating == 'half'){echo 'checked';}else{echo '';};
echo '/> 0.5 Star';

echo '<input type="radio" name="star_rating" value="one"';
if($star_rating == 'one'){echo 'checked';}else{echo '';};
echo '/> 1 Star';

echo '<input type="radio" name="star_rating" value="one-half"';
if($star_rating == 'one-half'){echo 'checked';}else{echo '';};
echo '/> 1.5 Star';

echo '<input type="radio" name="star_rating" value="two"';
if($star_rating == 'two'){echo 'checked';}else{echo '';};
echo '/> 2 Star';

echo '<input type="radio" name="star_rating" value="two-half"';
if($star_rating == 'two-half'){echo 'checked';}else{echo '';};
echo '/> 2.5 Star';

echo '<input type="radio" name="star_rating" value="three"';
if($star_rating == 'three'){echo 'checked';}else{echo '';};
echo '/> 3 Star';

echo '<input type="radio" name="star_rating" value="three-half"';
if($star_rating == 'three-half'){echo 'checked';}else{echo '';};
echo '/> 3.5 Star';

echo '<input type="radio" name="star_rating" value="four"';
if($star_rating == 'four'){echo 'checked';}else{echo '';};
echo '/> 4 Star';

echo '<input type="radio" name="star_rating" value="four-half"';
if($star_rating == 'four-half'){echo 'checked';}else{echo '';};
echo '/> 4.5 Star';

echo '<input type="radio" name="star_rating" value="five"';
if($star_rating == 'five'){echo 'checked';}else{echo '';};
echo '/> 5 Star';

These are the corresponding values I want to echo, so if 'three star' is checked' I want image logo-three-star.png to show:
<img src="<?php bloginfo('template_url'); ?>/images/logo-star-one.png" title="1 star">
<img src="<?php bloginfo('template_url'); ?>/images/logo-star-one-half.png" title="1.5 star">
<img src="<?php bloginfo('template_url'); ?>/images/logo-star-two.png" title="2 star">
<img src="<?php bloginfo('template_url'); ?>/images/logo-star-two-half.png" title="2.5 star">
<img src="<?php bloginfo('template_url'); ?>/images/logo-star-three.png" title="3 star">
<img src="<?php bloginfo('template_url'); ?>/images/logo-star-three-half.png" title="3.5 star">
<img src="<?php bloginfo('template_url'); ?>/images/logo-star-four.png" title="4 star">
<img src="<?php bloginfo('template_url'); ?>/images/logo-star-four-half.png" title="4.5 star">
<img src="<?php bloginfo('template_url'); ?>/images/logo-star-five.png" title="5 star">

Step 2: echo the values of a Checkbox
<label for="accreditations">' . __("Accreditations", 'myplugin_textdomain' ) . '</label>';

echo '<input type="checkbox" name="accreditations" value="aaa_tourism"';
if($accreditations == 'aaa_tourism'){echo 'checked';}else{echo '';};
echo' /> AAA Tourism';

echo '<input type="checkbox" name="accreditations" value="accredited_tourism_business_australia"';
if($accreditations == 'accredited_tourism_business_australia'){echo 'checked';}else{echo '';};
echo '/> Accredited Tourism Business Australia ';

echo '<input type="checkbox" name="accreditations" value="environmentally_friendly"';
if($accreditations == 'environmentally_friendly'){echo 'checked';}else{echo '';};
echo '/> Environmentally Friendly';

These are the corresponding values I want to echo, so if 'Accredited Tourism Business Australia' is checked' I want image logo-atba.png to show:
<img src="<?php bloginfo('template_url'); ?>/images/logo-aaa-tourism.png" title="AAA Tourism">
<img src="<?php bloginfo('template_url'); ?>/images/logo-atba.png" title="Accredited Tourism Business Australia">
<img src="<?php bloginfo('template_url'); ?>/images/logo-environmentally-friendly.png" title="Environmentally Friendly">

Just a reminder I am after the full code please.

Answers (1)


Denzel Chia answers:


Put this in your single.php, where you want to echo the stars.
assuming that your post meta key is star_rating.
Which means you used example; update_post_meta($post_id,'star_rating',$star_rating)


//for echo out star_rating
//if your post_meta key is not star_rating, please change accordingly.

global $post;
$post_id = $post->ID;
$star_rating = get_post_meta($post_id,'star_rating',true);
$template_url = get_bloginfo('template_url');

switch ($star_rating) {
case "none":
echo ""; //show nothing
case "half":
echo "<img src='$template_url/images/logo-half.png' title='0.5 star'>"; // show half star. you should provide the image
case "one":
echo "<img src='$template_url/images/logo-star-one.png' title='1 star'>";
case "one-half":
echo "<img src='$template_url/images/logo-star-one-half.png' title='1.5 star'>";
case "two":
echo "<img src='$template_url/images/logo-star-two.png' title='2 star'>";
case "two-half":
echo "<img src='$template_url/images/logo-star-two-half.png' title='2.5 star'>";
case "three":
echo "<img src='$template_url/images/logo-star-three.png' title='3 star'>";
case "three-half":
echo "<img src='$template_url/images/logo-star-three-half.png' title='3.5 star'>";
case "four":
echo "<img src='$template_url/images/logo-star-four.png' title='4 star'>";
case "four-half":
echo "<img src='$template_url/images/logo-star-four-half.png' title='4.5 star'>";
case "five":
echo "<img src='$template_url/images/logo-star-five.png' title='5 star'>";

I will come up with the second part now.


Denzel Chia comments:

For your second part. Accreditations.


//for echo out accreditations
//if your post_meta key is not accreditations, please change accordingly.

global $post;
$post_id = $post->ID;
$accreditations = get_post_meta($post_id,'accreditations',true); //post meta key
$template_url = get_bloginfo('template_url');

switch ($accreditations) {
case "aaa_tourism":
echo "<img src='$template_url/images/logo-aaa-tourism.png' title='AAA Tourism'>";
case "accredited_tourism_business_australia":
echo "<img src='$template_url/images/logo-atba.png' title='Accredited Tourism Business Australia'>"; break;
case "environmentally_friendly":
echo "<img src='$template_url/images/logo-environmentally-friendly.png' title='Environmentally Friendly'>";