Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.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
Php 如何将所有类别添加到数组中,提取类别名称并将其与自定义元框一起使用_Php_Arrays_Wordpress_Categories_Meta Boxes - Fatal编程技术网

Php 如何将所有类别添加到数组中,提取类别名称并将其与自定义元框一起使用

Php 如何将所有类别添加到数组中,提取类别名称并将其与自定义元框一起使用,php,arrays,wordpress,categories,meta-boxes,Php,Arrays,Wordpress,Categories,Meta Boxes,基本上,我需要添加自定义元框的网页,其中一个选项必须是从以前的文章类别列表。我已经成功地添加了元盒,并将类别包装在一个数组中,但是在页面上我肯定遗漏了什么,只显示了类别名称的第一个字母。以下是结果的屏幕截图: 我不知道我错过了什么。以下是metabox的代码: // Check if is page-homepage.php template $post_id = (isset($_GET['post'])) ? $_GET['post'] : ((is

基本上,我需要添加自定义元框的网页,其中一个选项必须是从以前的文章类别列表。我已经成功地添加了元盒,并将类别包装在一个数组中,但是在页面上我肯定遗漏了什么,只显示了类别名称的第一个字母。以下是结果的屏幕截图:

我不知道我错过了什么。以下是metabox的代码:

            // Check if is page-homepage.php template
        $post_id = (isset($_GET['post'])) ? $_GET['post'] : ((isset($_POST['post_ID'])) ? $_POST['post_ID'] : false);

        if ($post_id) : 
        $post_template = get_post_meta($post_id, '_wp_page_template', true);
        if ($post_template == 'page-homepage.php') {

        // Add the Meta Box
        function home_custom_meta_box() {
            add_meta_box(
                'home_meta_box', // $id
                'Custom Meta Box', // $title 
                'home_show_custom_meta_box', // $callback
                'page', // $page
                'normal', // $context
                'high'); // $priority
        }
        add_action('add_meta_boxes', 'home_custom_meta_box');

        // Field Array
        $prefix = 'custom_';
        $home_custom_meta_fields = array(
            array(
                'label' => 'Title for the middle column',
                'desc'  => 'Optional',
                'id'    => $prefix.'title1',
                'type'  => 'text'
            ),
                array(
                'label' => 'Category',
                'id'    => $prefix.'category',
                'type'  => 'select',
                'options' => $wp_cats
            ),
            array(
                'label' => 'Title for the right column',
                'desc'  => 'Optional',
                'id'    => $prefix.'title2',
                'type'  => 'text'
            ),
            array(
                'label' => 'Textarea',
                'desc'  => 'A description for the field.',
                'id'    => $prefix.'textarea',
                'type'  => 'textarea'
            ),
            array(
                'label' => 'Checkbox Input',
                'desc'  => 'A description for the field.',
                'id'    => $prefix.'checkbox',
                'type'  => 'checkbox'
            ),
            array(
                'label' => 'Select Box',
                'desc'  => 'A description for the field.',
                'id'    => $prefix.'select',
                'type'  => 'select',
                'options' => array (
                    'one' => array (
                        'label' => 'Option One',
                        'value' => 'one'
                    ),
                    'two' => array (
                        'label' => 'Option Two',
                        'value' => 'two'
                    ),
                    'three' => array (
                        'label' => 'Option Three',
                        'value' => 'three'
                    )
                )
            ),
            array (
                'label' => 'Radio Group',
                'desc'  => 'A description for the field.',
                'id'    => $prefix.'radio',
                'type'  => 'radio',
                'options' => array (
                    'one' => array (
                        'label' => 'Option One',
                        'value' => 'one'
                    ),
                    'two' => array (
                        'label' => 'Option Two',
                        'value' => 'two'
                    ),
                    'three' => array (
                        'label' => 'Option Three',
                        'value' => 'three'
                    )
                )
            ),
            array (
                'label' => 'Checkbox Group',
                'desc'  => 'A description for the field.',
                'id'    => $prefix.'checkbox_group',
                'type'  => 'checkbox_group',
                'options' => array (
                    'one' => array (
                        'label' => 'Option One',
                        'value' => 'one'
                    ),
                    'two' => array (
                        'label' => 'Option Two',
                        'value' => 'two'
                    ),
                    'three' => array (
                        'label' => 'Option Three',
                        'value' => 'three'
                    )
                )
            ),
            array(
                'label' => 'Post List',
                'desc'  => 'A description for the field.',
                'id'    =>  $prefix.'post_id',
                'type'  => 'post_list',
                'post_type' => array('post','page')
            ),
        );

        // The Callback
        function home_show_custom_meta_box() {
            global $home_custom_meta_fields, $post;
            // Use nonce for verification
            echo '<input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';

            // Begin the field table and loop
            echo '<table class="form-table">';
            foreach ($home_custom_meta_fields as $field) {
                // get value of this field if it exists for this post
                $meta = get_post_meta($post->ID, $field['id'], true);
                // begin a table row with
                echo '<tr>
                        <th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
                        <td>';
                        switch($field['type']) {
                            // text
                            case 'text':
                                echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
                                        <br /><span class="description">'.$field['desc'].'</span>';
                            break;
                            // textarea
                            case 'textarea':
                                echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea>
                                        <br /><span class="description">'.$field['desc'].'</span>';
                            break;
                            // checkbox
                            case 'checkbox':
                                echo '<input type="checkbox" name="'.$field['id'].'" id="'.$field['id'].'" ',$meta ? ' checked="checked"' : '','/>
                                        <label for="'.$field['id'].'">'.$field['desc'].'</label>';
                            break;
                            // select
                            case 'select':
                                echo '<select name="'.$field['id'].'" id="'.$field['id'].'">';
                                foreach ($field['options'] as $option) {
                                    echo '<option', $meta == $option['value'] ? ' selected="selected"' : '', ' value="'.$option['value'].'">'.$option['label'].'</option>';
                                }
                                echo '</select><br /><span class="description">'.$field['desc'].'</span>';
                            break;
                            // radio
                            case 'radio':
                                foreach ( $field['options'] as $option ) {
                                    echo '<input type="radio" name="'.$field['id'].'" id="'.$option['value'].'" value="'.$option['value'].'" ',$meta == $option['value'] ? ' checked="checked"' : '',' />
                                            <label for="'.$option['value'].'">'.$option['label'].'</label><br />';
                                }
                                echo '<span class="description">'.$field['desc'].'</span>';
                            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;
                            // tax_select
                            case 'tax_select':
                                echo '<select name="'.$field['id'].'" id="'.$field['id'].'">
                                        <option value="">Select One</option>'; // Select One
                                $terms = get_terms($field['id'], 'get=all');
                                $selected = wp_get_object_terms($post->ID, $field['id']);
                                foreach ($terms as $term) {
                                    if (!empty($selected) && !strcmp($term->slug, $selected[0]->slug)) 
                                        echo '<option value="'.$term->slug.'" selected="selected">'.$term->name.'</option>'; 
                                    else
                                        echo '<option value="'.$term->slug.'">'.$term->name.'</option>'; 
                                }
                                $taxonomy = get_taxonomy($field['id']);
                                echo '</select><br /><span class="description"><a href="'.get_bloginfo('home').'/wp-admin/edit-tags.php?taxonomy='.$field['id'].'">Manage '.$taxonomy->label.'</a></span>';
                            break;
                            // post_list
                            case 'post_list':
                            $items = get_posts( array (
                                'post_type' => $field['post_type'],
                                'posts_per_page' => -1
                            ));
                                echo '<select name="'.$field['id'].'" id="'.$field['id'].'">
                                        <option value="">Select One</option>'; // Select One
                                    foreach($items as $item) {
                                        echo '<option value="'.$item->ID.'"',$meta == $item->ID ? ' selected="selected"' : '','>'.$item->post_type.': '.$item->post_title.'</option>';
                                    } // end foreach
                                echo '</select><br /><span class="description">'.$field['desc'].'</span>';
                            break;
                            // date
                            case 'date':
                                echo '<input type="text" class="datepicker" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
                                        <br /><span class="description">'.$field['desc'].'</span>';
                            break;
                            // slider
                            case 'slider':
                            $value = $meta != '' ? $meta : '0';
                                echo '<div id="'.$field['id'].'-slider"></div>
                                        <input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$value.'" size="5" />
                                        <br /><span class="description">'.$field['desc'].'</span>';
                            break;
                            // image
                            case 'image':
                                $image = get_template_directory_uri().'/images/image.png';  
                                echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';
                                if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }               
                                echo    '<input name="'.$field['id'].'" type="hidden" class="custom_upload_image" value="'.$meta.'" />
                                            <img src="'.$image.'" class="custom_preview_image" alt="" /><br />
                                                <input class="custom_upload_image_button button" type="button" value="Choose Image" />
                                                <small>&nbsp;<a href="#" class="custom_clear_image_button">Remove Image</a></small>
                                                <br clear="all" /><span class="description">'.$field['desc'].'</span>';
                            break;
                            // repeatable
                            case 'repeatable':
                                echo '<a class="repeatable-add button" href="#">+</a>
                                        <ul id="'.$field['id'].'-repeatable" class="custom_repeatable">';
                                $i = 0;
                                if ($meta) {
                                    foreach($meta as $row) {
                                        echo '<li><span class="sort hndle">|||</span>
                                                    <input type="text" name="'.$field['id'].'['.$i.']" id="'.$field['id'].'" value="'.$row.'" size="30" />
                                                    <a class="repeatable-remove button" href="#">-</a></li>';
                                        $i++;
                                    }
                                } else {
                                    echo '<li><span class="sort hndle">|||</span>
                                                <input type="text" name="'.$field['id'].'['.$i.']" id="'.$field['id'].'" value="" size="30" />
                                                <a class="repeatable-remove button" href="#">-</a></li>';
                                }
                                echo '</ul>
                                    <span class="description">'.$field['desc'].'</span>';
                            break;
                        } //end switch
                echo '</td></tr>';
            } // end foreach
            echo '</table>'; // end table
        }

        // Save the Data
        function home_save_custom_meta($post_id) {
            global $home_custom_meta_fields;

            // verify nonce
            if (!wp_verify_nonce($_POST['custom_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;
            }

            // loop through fields and save the data
            foreach ($home_custom_meta_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);
                }
            } // enf foreach
        }
        add_action('save_post', 'home_save_custom_meta');

        }
下面是我对$wp_cats阵列的期望:

'options' => array (
    'one' => array (
        'label' => 'Option One',
        'value' => 'one'
    ),
    'two' => array (
        'label' => 'Option Two',
        'value' => 'two'
    ),
    'three' => array (
        'label' => 'Option Three',
        'value' => 'three'
    )
)
以下是一张包含详细信息的图片:

问题在于类别如何存储在与元框代码不匹配的数组中。为了解决这个问题,我使用了一个函数(由Jonathan Kuhn提供),它以一种可以与mate box代码一起使用的方式包装类别。下面是函数:

// Wrap all categories in a function
function wcat2() {
    $out = array();
    $categories = get_categories();
    foreach( $categories as $category ) {
        $out[$category->term_id] = array(
            'label' => $category->slug,
            'value' => $category->term_id
        );
    }
    //return array('options'=>$out);
    return $out;
}
此功能应替换此代码:

 // Get all categories from WP and create a dropdown
    $categories = get_categories('hide_empty=0&orderby=name');
    $wp_cats = array();
    foreach ($categories as $category_list ) {
           $wp_cats[$category_list->cat_ID] = $category_list->cat_name;
    }
    array_unshift($wp_cats, "Choose a category");
现在,您只需从元框中的数组调用函数:

    array(
            'label' => 'Category',
            'id'    => $prefix.'category',
            'type'  => 'select',
            'options' => wcat2()
        ),

通过这种方式,您可以拥有一个元框,该元框将从WordPress中绘制所有使用过的类别,这样用户就可以从元框选项中选择类别,而不必编写类别名称。

我无法对代码进行排序以查看发生了什么,但看起来您是在为字符串而不是数组编制索引<代码>$s='Test';echo$s['desc']输出'T',因为它获取字符串的第一个字符(我假设字符串索引的计算结果为int 0,而不是导致错误)。但当我打印(print_r)数组(在本例中为$wp_cats)时,它会给我一个键和一个值,例如array([0]=>Blog[1]=>有些东西……这正是让我困惑的地方。更不用说我使用了上面的代码
//从WP获取所有类别并创建一个下拉菜单$categories=Get_categories('hide_empty=0&orderby=name');$WP_cats=array();foreach($categories as$categories_list){$WP_cates[$categories_list->cat_ID]=$category\u list->cat\u name;}array\u unshift($wp\u cats,“选择一个类别”);
以前工作得很好。我帮不了你多少忙,因为我不知道系统是什么(我假设是WordPress)期望$wp_cats结构。根据我的经验,每当我遇到这种情况时,都是因为我将字符串视为数组。您可能应该在这个问题上添加wordpress标签。无论如何,我感谢您的帮助。我添加了更多信息,希望有人能够理解并能够提供帮助。
    array(
            'label' => 'Category',
            'id'    => $prefix.'category',
            'type'  => 'select',
            'options' => wcat2()
        ),