1
所以我有一個複選框的元框,我可以用它作爲開關打開某些內容。 現在只是回聲「OK!」和「不工作......」,取決於是否勾選複選框。 我的目標是迴應來自不同價值的不同類型的信息。如何從wordpress元複選框回顯多個值?
例如,其中一間公寓有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!」和「不工作......」。
旁註:像高級自定義字段插件將加快元盒和自定義字段的創建。 – brasofilo
我已經將函數custom_content_all($ post_id)更改爲函數custom_content_all($ id),它現在可以完美工作。非常感謝你!!! – user2863536