【发布时间】:2013-10-12 11:11:30
【问题描述】:
所以我有一个带有复选框的元框,我可以将其用作打开某些内容的开关。 现在它只是回声“OK!”和“不工作......”取决于是否选中了复选框。 我的目标是回应不同价值的不同类型的信息。
例如,其中一间公寓有 Wi-Fi,所以我需要在管理面板中检查“Wi-Fi”,以便在页面上显示 Wi-Fi 图标。
示例: apartments for rent website
他们获得了每个主要功能的图标here
这是functions.php中的代码:
$fieldsCheckbox = array(
'first' => 'First label',
'second' => 'Second label',
'third' => 'Third label'
);
add_action("admin_init", "checkbox_init");
function checkbox_init(){
add_meta_box("checkbox", "Checkbox", "checkbox", "post", "normal", "high");
}
function checkbox(){
global $post, $fieldsCheckbox;
$content = '';
foreach( $fieldsCheckbox as $fieldName => $fieldLabel) {
$content .= '<label>' . $fieldLabel;
$checked = get_post_meta($post->ID, $fieldName, true) ? 'checked="checked"' : '';
$content .= '<input type="checkbox" name="' . $fieldName . '" value=1 '. $checked .' />';
$content .= '</label><br />';
}
echo $content;
}
// Save Meta
add_action('save_post', 'save_details');
function save_details(){
global $post, $fieldsCheckbox;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post->ID;
}
foreach( $fieldsCheckbox as $fieldName => $fieldLabel) {
update_post_meta($post->ID, $fieldName, $_POST[$fieldName]);
}
}
function custom_content_all($id) {
global $fieldsCheckbox;
foreach( $fieldsCheckbox as $fieldName => $fieldLabel ) {
$fieldValue = get_post_meta($id, $fieldName, true);
if( !empty($fieldValue) ) {
echo "OK!";
}
else{
echo 'Not working...';
}
}
}
function custom_content_by_name($id, $name) {
$field_id = get_post_meta($id, $name, true);
if( !empty($field_id) ) {
echo "OK!";
}
else{
echo 'Not working...';
}
}
我用它在模板中调用它。
<?php custom_content_all(get_the_ID()); ?>
一切正常,但不是我想要的方式,我想知道如何更改此代码以在页面上回显不同的信息。
例如,我必须在管理面板中选中“第一个标签”以回显页面上的第一张图片。然后我必须在管理面板中检查“第二个标签”以回显第二张图片......等等。但是现在所有这些值都只回显“OK!”和“不工作......”。
【问题讨论】:
标签: php wordpress checkbox meta-boxes