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

Post edit background meta checkbox WordPress

  • SOLVED

Hi,

I am trying to make a background checkbox so I can select which type of background i have for my posts.

The choice is map, image, flv.

I am using the meta script from here
[[LINK href="http://www.deluxeblogtips.com/2011/03/meta-box-script-update-v30.html"]]
http://www.deluxeblogtips.com/2011/03/meta-box-script-update-v30.html[[/LINK]]

My problem is i cant seem to get the right code to display the correct result when one of the background choices has been selected from the post edit screen.

Here is my meta checkbox from meta-usage.php


$meta_boxes[] = array(
'id' => 'Background options',
'title' => 'Background options',
'pages' => array('post', 'film', 'page'),
'fields' => array(
array(
'name' => 'You must choose a background',
'id' => $prefix . 'background',
'type' => 'checkbox_list',
'options' => array(
'video' => 'video',
'geomap' => 'Map',
'img' => 'Image',
),
'desc' => 'Choose your background'
)
)
);


And this is my code that i am using to ba able to display the results when one of these checkboxes is selected..


<?php
$checkbox_list = get_post_meta(get_the_ID(), 'meta_key', false);
// if you want to show values
foreach ($checkbox_list as $checkbox_list) {
echo $checkbox_list;
}
// if you want to show values
?>

<?php if (in_array('geomap', $checkbox_list)) { ?>

echo ‘do stuff’;

<?php } elseif (in_array('img', $checkbox_list)) { ?>

echo ‘do stuff’;

<?php } elseif (in_array('flv', $checkbox_list)) { ?>

echo ‘do stuff’;

<?php } ?>



Hope this all makes sense ? I will be eternally grateful for anybodys help with this. I have been tinkering around with this for 3 days now - so am ready to pay for someones expert help.

best wishes,

tom

Answers (5)

2011-09-30

Fahad Murtaza answers:

I have recently used this one. I am on it. Just a few mins :)


Tom Mills comments:

oh awesome thanks :)


Fahad Murtaza comments:

Did you set any value for $prefix

If so, what is that?


Fahad Murtaza comments:

I see your field name is

$prefix . 'background',

Lets suppose that the prefix value is 'prefixvalue_' then the following code will give you the value of background field

<?
$bgvalue = get_post_meta(get_post_parent_ID(), 'prefixvalue_background', false);
echo $bgvalue;

?>


Let me know if it helps. I am doing exactly the same with no problem.

that


Fahad Murtaza comments:

Now lets use the $bgvalue variable for your specific need

<?php if ($bgvalue=='geomap') { ?>



echo ‘do stuff’;



<?php } elseif ($bgvalue=='video') { ?>



echo ‘do stuff’;



<?php } elseif ($bgvalue=='img') { ?>



echo ‘do stuff’;

<?php } ?>

<?php } ?>


Tom Mills comments:

The prefix is dtb_

$prefix = 'dbt_';

Hope this helps,

best wishes,

tom


Fahad Murtaza comments:

Here is the updated code based on your prefix value


<?php
$bgvalue = get_post_meta(get_post_parent_ID(), 'dtb_background', false);
echo $bgvalue;
?>
<?php if ($bgvalue=='geomap') { ?>
echo 'do stuff';
<?php } elseif ($bgvalue=='video') { ?>
echo 'do stuff';
<?php } elseif ($bgvalue=='img') { ?>
echo 'do stuff';
<?php } ?>
?>


Tom Mills comments:

Thanks - can i ask where does bgvalue come from ?

I have put in this code but i get

Call to undefined function get_post_parent_id()

thanks and best wishes,

tom


Fahad Murtaza comments:

Sorry, my bad. try the following

<?php
$bgvalue = get_post_meta(get_the_ID(), 'dtb_background', false);
echo $bgvalue;
?>
<?php if ($bgvalue=='geomap') { ?>
echo 'do stuff';
<?php } elseif ($bgvalue=='video') { ?>
echo 'do stuff';
<?php } elseif ($bgvalue=='img') { ?>
echo 'do stuff';
<?php } ?>
?>



Fahad Murtaza comments:

The $bgvalue comes from the metabox that you have created using the library.


Tom Mills comments:

Thanks I have tried the code above - oddly 'Array' gets printed on the webpage. But it does not work i am afraid :(

This is what it says on delux blog tips for checking checkbox and display

Get value of field with multiple values

There’re some fields that may have multiple values such as select, checkbox_list. To get (or check) values of these fields, use this code:

$metas = get_post_meta(get_the_ID(), 'meta_key', false);

// if you want to show values
foreach ($metas as $meta) {
echo $meta;
}

// if you want to check if a value $val is set or not
if (in_array($val, $metas)) {
echo "$val is set";
} else {
echo "$val is not set";
}


and here is my metabox

$meta_boxes[] = array(

'id' => 'Background options',

'title' => 'Background options',

'pages' => array('post', 'film', 'page'),

'fields' => array(

array(

'name' => 'You must choose a background',

'id' => $prefix . 'background',

'type' => 'checkbox_list',

'options' => array(

'video' => 'video',

'geomap' => 'Map',

'img' => 'Image',

),

'desc' => 'Choose your background'

)

)

);



I cant see bgvalue here - does it come from somwhere else in the meta scipt ?

thanks and ebst wishes,

tom


Fahad Murtaza comments:

Hi


I am going to test it locally and recreating the whole sample for you.

Just a few more mins.


Fahad Murtaza comments:

Also I am assuming you are already able to see the metabox in your wordpress admin when you create/edit post. And you are also able to the save the value for the background.

My code just helps you with the front end which gets the value for a single value field called 'dtb_background' as per your defined metabox using the library.

I sent you a PM. Please check.


Tom Mills comments:

HI All,

Fahd helped me find answer over the weekend. Hopefully this will be helpful to others.

<?php

// The follwing line just checks if the post ID being retrieved is correct. If this matches the post ID from
//observing the ID in wp-admin, then its correct
//echo get_the_ID();


// Get the post meta data for custom field i.e dbt_background which is $prefix and ''background''concatenated
$checkbox_list = get_post_meta(get_the_ID(), 'dbt_background', false);

// Checking if the array is not empty. Anything above 0 means there are one or more checkboxes that were selected
//echo "Total checkboxes =" .count($checkbox_list);

//// if you want to show values, for testing

//foreach ($checkbox_list as $checkbox_list) {
//print_r($checkbox_list);
// echo $checkbox_list;
//}

// The following line outputs every single meta key and its calue
//the_meta(); ?>


<?php if (in_array('geomap', $checkbox_list)) {
echo "1";
?>

do something


<?php } elseif (in_array('img', $checkbox_list)) {
echo "2";
?>

do something else


<?php } elseif (in_array('pano', $checkbox_list)) { ?>

or do this


<?php } ?>


Thanks for everybodies help with this one - it is really appreciated. WP Questions is new to me, so not quite worked out best way to reward good answers and the person that did the work.

I will work it out and defo be back. Many thanks again :)

tom

2011-09-30

Julio Potier answers:

Hello

i can do this for you, but for a easily way to do this, i need an admin access to the website with editor rights.

i'm you man :)

See you soon

2011-09-30

John Cotton answers:

I don't use that code, but this line looks suspiciously wrong:


$checkbox_list = get_post_meta(get_the_ID(), 'meta_key', false);


'meta_key' should be the actual name of the key. from your code, my guess is that is $prefix.'background', but I don't know what $prefix might be.....

Look in the wp_postmeta table and you should find the actual name. Swap that in and...who knows!!

2011-09-30

Jens Filipsson answers:

If it's supposed to change your body backgroud, then you could replace your body tag with the code. If it's a different div you want for different backgrounds, then replace the body tag with the div tag you want...

<?php

$checkbox_list = get_post_meta(get_the_ID(), 'meta_key', false);

// if you want to show values

foreach ($checkbox_list as $checkbox_list) {

echo $checkbox_list;

}

// if you want to show values

?>

<?php if (in_array('geomap', $checkbox_list)) { ?>

echo '<body backgroud="http://path.to/background.jpg">';

<?php } elseif (in_array('img', $checkbox_list)) { ?>

echo '<body backgroud="http://path.to/background.jpg">';

<?php } elseif (in_array('flv', $checkbox_list)) { ?>

echo '<body backgroud="http://path.to/background.jpg">';

<?php } ?>

2011-09-30

Abdessamad Idrissi answers:

hey tom,
you will need a custom plugin that integrates AJAx and a [[LINK href="http://www.eyecon.ro/colorpicker/"]]jQuery color picker[[/LINK]]. pm me and I'll be happy to help you get this done :)