0

我刚刚使用wptuts上的教程创建了一种长元框,但我迷失了两件事:

  1. 元框的输出显示所有分组的数据(例如,在分组复选框中,即使我有不同的组,它们也是一个整体),我想获取每个组的标题并将它们显示在不同的表、div 或块中.

  2. 不知道如何在帖子中显示保存的数据,并且将其显示在单独的块中。

我的代码是这样的:

<?php

$prefix = 'dbt_';
$meta_box = array(
'id' => 'features',
'title' => 'Property Features',
'page' => 'post',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
    array(
        'name' => 'Price (in USD)',
        'desc' => '',
        'id' => $prefix . 'text',
        'type' => 'text',
        'std' => ' '
    ),

      array(
        'name' => 'Price (in Mexican Peso)',
        'desc' => '',
        'id' => $prefix . 'text',
        'type' => 'text',
        'std' => ' '
    ),

    array(
        'name' => 'Address',
        'desc' => '',
        'id' => $prefix . 'textarea',
        'type' => 'textarea',
        'std' => ''
    ),

    array(
        'name' => 'Property for',
        'id' => $prefix . 'select',
        'type' => 'select',
        'options' => array('Sale', 'Rent', 'Vacational Rental')
    ),

    array(
        'name' => 'Area (m2)',
        'desc' => '',
        'id' => $prefix . 'text',
        'type' => 'text',
        'std' => ' '
    ),

    array(
        'name' => 'Area of terrain (m2)',
        'desc' => '',
        'id' => $prefix . 'text',
        'type' => 'text',
        'std' => ' '
    ),

    array(
        'name' => 'Number of rooms',
        'desc' => '',
        'id' => $prefix . 'text',
        'type' => 'text',
        'std' => ' '
    ),

    array(
        'name' => 'Number of bathrooms. Include half bathrooms (ex. 2 1/2)',
        'desc' => '',
        'id' => $prefix . 'text',
        'type' => 'text',
        'std' => ' '
    ),

    array(
        'name' => 'Service Bathroom',
        'id' => $prefix . 'checkbox',
        'type' => 'checkbox'
),

array(
        'name' => 'Furnished',
        'id' => $prefix . 'checkbox',
        'type' => 'checkbox'
),
array(  
        'label' => 'Property specs',  
        'desc'  => '',  
        'id'    => $prefix.'checkbox_group',  
        'type'  => 'checkbox_group',  
        'options' => array (  
        'one' => array (  
        'label' => 'Livingroom',  
        'value' => 'one'  
    ),  
        'two' => array (  
        'label' => 'Diningroom',  
        'value' => 'two'  
    ),  
        'three' => array (  
        'label' => 'Breackfast area',  
        'value' => 'three'  
    ), 

        'four' => array (  
        'label' => 'TV room',  
        'value' => 'four'  
    ),

        'five' => array (  
        'label' => 'Studio',  
        'value' => 'five'  
    ),

        'six' => array (  
        'label' => 'Integrated Kitchen',  
        'value' => 'six'  
    ),

        'seven' => array (  
        'label' => 'Laundry Room',  
        'value' => 'seven'  
    ),

        'eight' => array (  
        'label' => 'Service room',  
        'value' => 'eight'  
    ),

        'nine' => array (  
        'label' => 'jacuzzi',  
        'value' => 'nine'  
    ),

        'ten' => array (  
        'label' => 'terrace',  
        'value' => 'ten'  
    ),

        'eleven' => array (  
        'label' => 'balcony',  
        'value' => 'eleven'  
    ),

        'twelve' => array (  
        'label' => 'Warehouse',  
        'value' => 'twelve'  
    )
)  
),  


    array(  
        'label' => 'Installed',  
        'desc'  => '',  
        'id'    => $prefix.'checkbox_group',  
        'type'  => 'checkbox_group',  
        'options' => array (  
        'one' => array (  
        'label' => 'Air conditioner',  
        'value' => 'one'  
    ),  
        'two' => array (  
        'label' => 'Stationary gas',  
        'value' => 'two'  
    ),  
        'three' => array (  
        'label' => 'Water heater',  
        'value' => 'three'  
    ), 

        'four' => array (  
        'label' => 'Hurricain shutters or hurricain protection',  
        'value' => 'four'  
    ),

        'five' => array (  
        'label' => 'Telephone line',  
        'value' => 'five'  
    )
)  
),

    array(  
        'label' => 'Outdoors',  
        'desc'  => '',  
        'id'    => $prefix.'checkbox_group',  
        'type'  => 'checkbox_group',  
        'options' => array (  
        'one' => array (  
        'label' => 'Parking space',  
        'value' => 'one'  
    ),  
        'two' => array (  
        'label' => 'Garden',  
        'value' => 'two'  
    ),  
        'three' => array (  
        'label' => 'Pool',  
        'value' => 'three'  
    ), 

        'four' => array (  
        'label' => 'Tennis court',  
        'value' => 'four'  
    ),

        'five' => array (  
        'label' => 'Children playground',  
        'value' => 'five'  
    )
)  
),

    array(  
        'label' => 'Facilities',  
        'desc'  => '',  
        'id'    => $prefix.'checkbox_group',  
        'type'  => 'checkbox_group',  
        'options' => array (  
        'one' => array (  
        'label' => 'Spa',  
        'value' => 'one'  
    ),  
        'two' => array (  
        'label' => 'Gym',  
        'value' => 'two'  
    ),  
        'three' => array (  
        'label' => 'Party salon',  
        'value' => 'three'  
    ), 

        'four' => array (  
        'label' => 'Snack-bar',  
        'value' => 'four'  
    )

)  
),

    array(  
        'label' => 'Luxury Features',  
        'desc'  => '',  
        'id'    => $prefix.'checkbox_group',  
        'type'  => 'checkbox_group',  
        'options' => array (  
        'one' => array (  
        'label' => 'Clubhouse',  
        'value' => 'one'  
    ),  
        'two' => array (  
        'label' => 'Dock',  
        'value' => 'two'  
    ),  
        'three' => array (  
        'label' => 'Waterfront',  
        'value' => 'three'  
    ), 

        'four' => array (  
        'label' => 'Golf course',  
        'value' => 'four'  
    )


)  
)

)
);


add_action('admin_menu', 'mytheme_add_box');
// Add meta box
function mytheme_add_box() {
global $meta_box;
add_meta_box($meta_box['id'], $meta_box['title'], 'mytheme_show_box', $meta_box['page'],    $meta_box['context'], $meta_box['priority']);
}

// Callback function to show fields in meta box
function mytheme_show_box() {
global $meta_box, $post;
// Use nonce for verification
echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<div class="form-table">';
foreach ($meta_box['fields'] as $field) {
    // get current post meta data
    $meta = get_post_meta($post->ID, $field['id'], true);
    echo '<tr>',
            '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
            '<td>';
    switch ($field['type']) {
        case 'text':
            echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', '<br />', $field['desc'];
            break;
        case 'textarea':
            echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', '<br />', $field['desc'];
            break;
        case 'select':
            echo '<select name="', $field['id'], '" id="', $field['id'], '">';
            foreach ($field['options'] as $option) {
                echo '<option ', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
            }
            echo '</select>';
            break;
        case 'radio':
            foreach ($field['options'] as $option) {
                echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
            }
            break;
        case 'checkbox':
            echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
            break;


// checkbox_group  
case 'checkbox_group':  
foreach ($field['options'] as $option) {  
    echo '<input type="checkbox" value="'.$option['value'].'" name="'.$field['id'].'[]" id="'.$option['value'].'"',$meta && in_array($option['value'], $meta) ? ' checked="checked"' : '',' />  
            <label for="'.$option['value'].'">'.$option['label'].'</label><br />';  
}  
echo '<span class="description">'.$field['desc'].'</span>';  
break;    

    }
    echo     '</td><td>',
        '</td></tr>';
}
echo '</table>';
}

do_action('save_post', 'mytheme_save_data');
// Save data from meta box
function mytheme_save_data($post_id) {
global $meta_box;
// verify nonce
if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
    return $post_id;
}
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
    return $post_id;
}
// check permissions
if ('page' == $_POST['post_type']) {
    if (!current_user_can('edit_page', $post_id)) {
        return $post_id;
    }
} elseif (!current_user_can('edit_post', $post_id)) {
    return $post_id;
}
foreach ($meta_box['fields'] as $field) {
    $old = get_post_meta($post_id, $field['id'], true);
    $new = $_POST[$field['id']];
    if ($new && $new != $old) {
        update_post_meta($post_id, $field['id'], $new);
    } elseif ('' == $new && $old) {
        delete_post_meta($post_id, $field['id'], $old);
    }
}
}

// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return $post_id;
}
4

1 回答 1

0
$some_var = get_post_meta($post->ID, 'name_of_post_meta_data_here', true);

echo $some_var;

这有帮助吗?阅读标记发布的链接。

编辑

$some_var

给它一个名字,比如 $currency, $address... 这可以是你想要的。

get_post_meta($post->ID),

这意味着它从当前帖子中获取元数据。

'name_of_post_meta_data_here', true);

这就是你说它需要放入变量的地方,所以你创建的自定义元框。true 是它输出一个字符串,false 是一个数组。

而不是你可以像这样回应它:

<?php echo $some_var; ?>

我认为您应该查看Advanced Custom FieldsPods Framework,它们完全符合您的要求,并且您需要较少的 PHP 知识来处理它。我不是 PHP 英雄,所以我可以联系起来 =)

于 2013-06-10T12:04:56.097 回答