检索存储在wordpress自定义metabox中的值

时间:2018-06-30 19:25:08

标签: wordpress

我创建了自定义帖子,还创建了metabox,它在wp-admin端工作正常,但是在检索值时,我无法检索值,除了此metabox正常之外,其他所有东西都无法正常工作,但是metabox值我无法检索它。请为此提供帮助

class team_member_Meta_Box {
    private $screens = array(
        'pesto_team_members',
    );
    private $fields = array(
        array(
            'id' => 'enter-team-member-name',
            'label' => 'Enter Team Member Name',
            'type' => 'text',
        ),
        array(
            'id' => 'enter-team-member-position',
            'label' => 'Enter Team Member Position',
            'type' => 'text',
        ),
        array(
            'id' => 'team-member-facebook-url',
            'label' => 'Team Member Facebook Url',
            'type' => 'url',
        ),
        array(
            'id' => 'team-member-twitter-url',
            'label' => 'Team Member Twitter Url',
            'type' => 'url',
        ),
        array(
            'id' => 'team-member-linkedin-url',
            'label' => 'Team Member LinkedIn Url',
            'type' => 'url',
        ),
    );

    /**
     * Class construct method. Adds actions to their respective WordPress hooks.
     */
    public function __construct() {
        add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
        add_action( 'save_post', array( $this, 'save_post' ) );
    }

    /**
     * Hooks into WordPress' add_meta_boxes function.
     * Goes through screens (post types) and adds the meta box.
     */
    public function add_meta_boxes() {
        foreach ( $this->screens as $screen ) {
            add_meta_box(
                'team-member-details',
                __( 'Team Member Details', 'pestopest' ),
                array( $this, 'add_meta_box_callback' ),
                $screen,
                'normal',
                'high'
            );
        }
    }

    /**
     * Generates the HTML for the meta box
     * 
     * @param object $post WordPress post object
     */
    public function add_meta_box_callback( $post ) {
        wp_nonce_field( 'team_member_details_data', 'team_member_details_nonce' );
        echo 'Enter Your team member details';
        $this->generate_fields( $post );
    }

    /**
     * Generates the field's HTML for the meta box.
     */
    public function generate_fields( $post ) {
        $output = '';
        foreach ( $this->fields as $field ) {
            $label = '<label for="' . $field['id'] . '">' . $field['label'] . '</label>';
            $db_value = get_post_meta( $post->ID, 'team_member_details_' . $field['id'], true );
            switch ( $field['type'] ) {
                default:
                    $input = sprintf(
                        '<input %s id="%s" name="%s" type="%s" value="%s">',
                        $field['type'] !== 'color' ? 'class="regular-text"' : '',
                        $field['id'],
                        $field['id'],
                        $field['type'],
                        $db_value
                    );
            }
            $output .= $this->row_format( $label, $input );
        }
        echo '<table class="form-table"><tbody>' . $output . '</tbody></table>';
    }

    /**
     * Generates the HTML for table rows.
     */
    public function row_format( $label, $input ) {
        return sprintf(
            '<tr><th scope="row">%s</th><td>%s</td></tr>',
            $label,
            $input
        );
    }
    /**
     * Hooks into WordPress' save_post function
     */
    public function save_post( $post_id ) {
        if ( ! isset( $_POST['team_member_details_nonce'] ) )
            return $post_id;

        $nonce = $_POST['team_member_details_nonce'];
        if ( !wp_verify_nonce( $nonce, 'team_member_details_data' ) )
            return $post_id;

        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
            return $post_id;

        foreach ( $this->fields as $field ) {
            if ( isset( $_POST[ $field['id'] ] ) ) {
                switch ( $field['type'] ) {
                    case 'email':
                        $_POST[ $field['id'] ] = sanitize_email( $_POST[ $field['id'] ] );
                        break;
                    case 'text':
                        $_POST[ $field['id'] ] = sanitize_text_field( $_POST[ $field['id'] ] );
                        break;
                }
                update_post_meta( $post_id, 'team_member_details_' . $field['id'], $_POST[ $field['id'] ] );
            } else if ( $field['type'] === 'checkbox' ) {
                update_post_meta( $post_id, 'team_member_details_' . $field['id'], '0' );
            }
        }
    }
}
new team_member_Meta_Box;

我创建了自定义帖子,还创建了metabox,它在wp-admin端工作正常,但是在检索值时,我无法检索值,除了此metabox正常之外,其他所有东西都无法正常工作,但是metabox值我无法检索它。请为此提供帮助

0 个答案:

没有答案