Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Checkbox 如何在wordpress元数据库中分离数据?_Checkbox_Wordpress Theming_Wordpress_Meta Boxes - Fatal编程技术网

Checkbox 如何在wordpress元数据库中分离数据?

Checkbox 如何在wordpress元数据库中分离数据?,checkbox,wordpress-theming,wordpress,meta-boxes,Checkbox,Wordpress Theming,Wordpress,Meta Boxes,我刚刚使用上的教程创建了一种长元盒,但我迷失在两件事上: metabox的输出显示所有分组的数据(例如,在分组复选框中,即使我有不同的组,它们都是一个整体),我想获取每个组的标题,并将它们显示在不同的表、分区或块中 不知道如何在帖子中显示保存的数据,也不知道如何在单独的块中显示 我的代码是: <?php $prefix = 'dbt_'; $meta_box = array( 'id' => 'features', 'title' => 'Property Features'

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

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

  • 不知道如何在帖子中显示保存的数据,也不知道如何在单独的块中显示

  • 我的代码是:

    <?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;
    }
    
    
    这有用吗?阅读链接标记张贴虽然

    编辑

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

    get_post_meta($post->ID),
    
    这意味着它从当前帖子中获取元数据

    'name_of_post_meta_data_here', true);
    
    在这里,您可以说明需要在变量中放置什么,从而创建自定义元框。true表示它输出一个字符串,false表示数组

    然后你可以像这样重复它:

    <?php echo $some_var; ?>
    
    
    

    我认为你应该去看看,或者,他们完全做你想要的,你需要更少的PHP知识来处理它。我不是一个PHP英雄,所以我可以关联=)

    你读过这篇文章吗?嗨,马克,是的,我读了一遍又一遍,但不知道如何划分每个区块。我还不太了解变量。也许我可以用它来划分块?你能解释一下如何使用变量吗?这是否有助于我在后端和前端显示块?谢谢Kortschot,我知道我需要为每个字段正确设置变量?那个代码是前端输出的,对吗?如何在后端的块中显示它们?很抱歉,我是一个傻瓜,但是我想学习如何为将来编写代码,而不是使用插件。我渴望知识!
    <?php echo $some_var; ?>