如何在Wordpress的自定义元框中添加帖子类别

时间:2018-07-16 08:47:10

标签: php wordpress woocommerce hook-woocommerce

我想在下拉选择框中加载我的帖子类别,请问我是如何编程的?

这是我的代码,用于创建自定义元框,现在在此处的类别描述中,我希望它在下拉菜单中加载所有帖子类别:代码的输出显示在所附的图像中。     

            //Start: Adding custom metaboxes

            class leaderboarddetailMetabox {
                private $screen = array(
                    'ld-leaderboard',
                );
                private $meta_fields = array(
                    array(
                        'label' => 'Number of Students',
                        'id' => 'numberofstudent_81418',
                        'type' => 'number',
                    ),
                    array(
                        'label' => 'Select Point Type',
                        'id' => 'selectpointtype_39141',
                        'type' => 'select',
                        'options' => array(
                            'Select',
                            'Int',
                            'Float',
                            'String',
                        ),
                    ),
                    array(
                        'label' => 'Category',
                        'id' => 'category_59112',
                        'type' => 'select',
                        'options' => array(
                            'Select',
                            'Course',
                            'Lesson',
                            'Topic',
                            'Quiz',
                            'Category',
                        ),
                    ),
                    array(
                        'label' => 'Time',
                        'id' => 'time_93126',
                        'type' => 'date',
                    ),
                    array(
                        'label' => 'Shortcode',
                        'id' => 'shortcode_85946',
                        'type' => 'text',
                        'readonly' => true,
                    ),
                );
                public function __construct() {
                    add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
                    add_action( 'save_post', array( $this, 'save_fields' ) );
                }
                // show meta boxes on admin page
                public function add_meta_boxes() {
                    foreach ( $this->screen as $single_screen ) {
                        add_meta_box(
                            'leaderboarddetail',
                            __( 'leader board detail', 'textdomain' ),
                            array( $this, 'meta_box_callback' ),
                            $single_screen,
                            'advanced',
                            'default'
                        );
                    }
                }
                public function meta_box_callback( $post ) {
                    wp_nonce_field( 'leaderboarddetail_data', 'leaderboarddetail_nonce' );
                    $this->field_generator( $post );
                }
                public function field_generator( $post ) {
                    $output = '';
                    foreach ( $this->meta_fields as $meta_field ) {
                        $label = '<label for="' . $meta_field['id'] . '">' . $meta_field['label'] . '</label>';
                        $meta_value = get_post_meta( $post->ID, $meta_field['id'], true );
                        if ( empty( $meta_value ) ) {
                            $meta_value = $meta_field['default']; }
                        switch ( $meta_field['type'] ) {
                            case 'select':
                                $input = sprintf(
                                    '<select id="%s" name="%s">',
                                    $meta_field['id'],
                                    $meta_field['id']
                                );
                                foreach ( $meta_field['options'] as $key => $value ) {
                                    $meta_field_value = !is_numeric( $key ) ? $key : $value;
                                    $input .= sprintf(
                                        '<option %s value="%s">%s</option>',
                                        $meta_value === $meta_field_value ? 'selected' : '',
                                        $meta_field_value,
                                        $value
                                    );
                                }
                                $input .= '</select>';
                                break;
                            default:
                                $input = sprintf(
                                    '<input %s id="%s" name="%s" type="%s" value="%s" %s >',
                                    $meta_field['type'] !== 'color' ? 'style="width: 100%"' : '',
                                    $meta_field['id'],
                                    $meta_field['id'],
                                    $meta_field['type'],
                                    $meta_value,
                                    $meta_field['readonly'] ? "readonly" : ""
                                );
                        }
                        $output .= $this->format_rows( $label, $input );
                    }
                    echo '<table class="form-table"><tbody>' . $output . '</tbody></table>';
                }
                public function format_rows( $label, $input ) {
                    return '<tr><th>'.$label.'</th><td>'.$input.'</td></tr>';
                }

                // save custom field data to databse and in the field 

                public function save_fields( $post_id ) {
                    if ( ! isset( $_POST['leaderboarddetail_nonce'] ) )
                        return $post_id;
                    $nonce = $_POST['leaderboarddetail_nonce'];
                    if ( !wp_verify_nonce( $nonce, 'leaderboarddetail_data' ) )
                        return $post_id;
                    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
                        return $post_id;
                    foreach ( $this->meta_fields as $meta_field ) {
                        if ( isset( $_POST[ $meta_field['id'] ] ) ) {
                            switch ( $meta_field['type'] ) {
                                case 'email':
                                    $_POST[ $meta_field['id'] ] = sanitize_email( $_POST[ $meta_field['id'] ] );
                                    break;
                                case 'text':
                                    $_POST[ $meta_field['id'] ] = sanitize_text_field( $_POST[ $meta_field['id'] ] );
                                    break;
                            }
                            update_post_meta( $post_id, $meta_field['id'], $_POST[ $meta_field['id'] ] );
                        } else if ( $meta_field['type'] === 'checkbox' ) {
                            update_post_meta( $post_id, $meta_field['id'], '0' );
                        }

                        // generate shortcode and save
                        if( $meta_field['id'] == 'shortcode_85946' ) {
                            // update meta
                            update_post_meta( $post_id, $meta_field['id'], "[custom-shortcode id=" . $post_id . "]");
                        }
                    }
                }
            }
            if (class_exists('leaderboarddetailMetabox')) {
                new leaderboarddetailMetabox;
            };
            ?>

1 个答案:

答案 0 :(得分:0)

要获取类别,可以使用get_terms()get_categories()

如果您使用默认帖子类别而不是自定义分类法,则您的功能应类似于:

$categories = get_categories( array(
    'orderby'    => 'name',
    'order'      => 'ASC',
    'hide_empty' => false
) );
$categoryNames = array();
if (is_array($categories)) {
    foreach ($categories as $category) {
        array_push($categoryNames, $category->name);
    }
}

这将更改您的$meta_fields变量,如下所示:

private $meta_fields = array(
    array(
        'label' => 'Number of Students',
        'id' => 'numberofstudent_81418',
        'type' => 'number',
    ),
    array(
        'label' => 'Select Point Type',
        'id' => 'selectpointtype_39141',
        'type' => 'select',
        'options' => array(
            'Select',
            'Int',
            'Float',
            'String',
        ),
    ),
    array(
        'label' => 'Category',
        'id' => 'category_59112',
        'type' => 'select',
        'options' => $categoryNames
    ),
    array(
        'label' => 'Time',
        'id' => 'time_93126',
        'type' => 'date',
    ),
    array(
        'label' => 'Shortcode',
        'id' => 'shortcode_85946',
        'type' => 'text',
        'readonly' => true,
    ),
);