How can I display a custom text in the loop, in case I’ve checked my Custom Field, checkbox style?
The plain
<?php echo get_post_meta($post->ID, \\"NAME OF YOUR CUSTOM FIELD\\", $single = true); ?>
just prints me true or false.
Thank you,
F
How can I display a custom text in the loop, in case I’ve checked my Custom Field, checkbox style?
The plain
<?php echo get_post_meta($post->ID, \\"NAME OF YOUR CUSTOM FIELD\\", $single = true); ?>
just prints me true or false.
Thank you,
F
I had a problem with this myself, I am not using the get_post_meta I am using get_custom_field plugin http://www.coffee2code.com/archives/2004/06/30/plugin-get-custom/ which is very easy to use.
I created a new function in get-custom.php (which is the get custom field plugin) on line 107 which is:
// Prints Value if Checkbox is true
function c2c_print_check($field, $text, $falseval=''){
if(c2c_get_custom($field) == 'true') {
echo $text;
} else {
echo $falseval;
}
}
// Prints check boxes
function c2c_print_checkboxes($fields, $between='')
{
$fieldvals = explode(\\\"/\\\", $fields);
for ($i=0; $i<count($fieldvals); $i++)
{
$field = $fieldvals[$i];
if (c2c_get_custom($field)==\\\"true\\\") {
if ($i>0) echo $between;
echo c2c_print_check($field, $field);
}
}
}
Then I add the check boxes to the conf file and put this in my html where I want the checkboxes to print:
<?php c2c_print_checkboxes('photoshop/css/html', ', ');?>
photoshop/css/html being the names of the check box fields.
Hope that helped.
Hey qwik3r2,
thank you very much for your help. Yes, I use the get_custom_field plugin, too.
I had to erase all the occurrences of triple backslahes \\\ in the code (certainly due to some formatting/escaping here in this forum), but apart from that, it worked like charm!
Now I can print the name of the check box field, when checked. Is there also a way to automatically add some code before and after, like in $before, $after etc.? Can I add those parameters to the function (but where and how)? I’m sorry if this is simple/obvious, I just have no clue of php.
Thanks, F.
You must log in to post.