Metabox数据不能保存

时间:2016-05-22 22:57:33

标签: wordpress wordpress-theming metadata

我在一个类中创建了一个元数据箱。我需要它用于我拥有的自定义帖子类型。元数据显示正常,但值拒绝保存。我已经多次修改了代码,但它仍然不适合我。你知道我做错了什么吗?这是完整的代码:

class MyClass {
public:
    MyClass() {}
    // note const specifier indicates method guarantees
    // no changes to class instance and noexcept specifier
    // tells compiler that this method is no-throw guaranteed
    int get_x() const noexcept { return x; }
    void set_x(int _x) { x = _x; }
private: 
    int x;
};

更新:

Mukesh Panchal给出的代码效果很好。它就像我想要的那样保存,但我仍然无法获取要在模板上显示的元数据。任何想法为什么会这样?

3 个答案:

答案 0 :(得分:1)

输入名称=“”属性中有一个额外的空格,例如:

name="portfolio_web_design "

这应该是:

name="portfolio_web_design"

之后你的isset应该正常工作;)

答案 1 :(得分:0)

您可以尝试下面更新的代码:

<?php
class portfolio_metabox {

    public function __construct() {
        if ( is_admin() ) {
            add_action( 'load-post.php',     array( $this, 'init_metabox' ) );
            add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );
        }
    }
    public function init_metabox() {
        add_action( 'add_meta_boxes',        array( $this, 'add_metabox' )         );
        add_action( 'save_post',             array( $this, 'save_metabox' ), 10, 2 );
    }

    public function add_metabox() {
        add_meta_box(
            'details',
            __( 'Project Details', 'chic' ),
            array( $this, 'render_project_metabox' ),
            'portfolio',
            'advanced',
            'default'
        );
    }

    public function render_project_metabox( $post ) {

        // Retrieve an existing value from the database.
        $portfolio_web_design  = get_post_meta( $post->ID, 'portfolio_web_design ', true );
        $portfolio_web_dev  = get_post_meta( $post->ID, 'portfolio_web_dev ', true );
        $portfolio_digital_art  = get_post_meta( $post->ID, 'portfolio_digital_art ', true );
        $portfolio_graphic_design  = get_post_meta( $post->ID, 'portfolio_graphic_design ', true );

        // Set default values.
        // Form fields.
        echo '<table class="form-table">';
        echo '  <tr>';
        echo '      <th><label for="portfolio_project" class="portfolio_project_label">' . __( 'Project Type', 'chic' ) . '</label></th>';
        echo '      <td>';
        echo '          <label><input type="checkbox" name="portfolio_web_design" class="portfolio_project_field" value="' . $portfolio_web_design  . '" ' . checked( $portfolio_web_design , 'checked', false ) . '> ' . __( ' Web Design', 'chic' ) . '</label><br>';
        echo '          <label><input type="checkbox" name="portfolio_web_dev" class="portfolio_project_field" value="' . $portfolio_web_dev  . '" ' . checked( $portfolio_web_dev , 'checked', false ) . '> ' . __( ' Web Development', 'chic' ) . '</label><br>';
        echo '          <label><input type="checkbox" name="portfolio_digital_art" class="portfolio_project_field" value="' . $portfolio_digital_art  . '" ' . checked( $portfolio_digital_art , 'checked', false ) . '> ' . __( ' Digital Art', 'chic' ) . '</label><br>';
        echo '          <label><input type="checkbox" name="portfolio_graphic_design" class="portfolio_project_field" value="' . $portfolio_graphic_design  . '" ' . checked( $portfolio_graphic_design , 'checked', false ) . '> ' . __( ' Graphic Design', 'chic' ) . '</label><br>';
        echo '          <p class="description">' . __( 'Project Type', 'chic' ) . '</p>';
        echo '      </td>';
        echo '  </tr>';
        echo '</table>';

    }

    public function save_metabox( $post_id, $post ) {

        // Sanitize user input.
        $portfolio_new_web_design = isset( $_POST[ 'portfolio_web_design' ] ) ? 'checked' : '';
        $portfolio_new_web_dev = isset( $_POST[ 'portfolio_web_dev' ] ) ? 'checked' : '';
        $portfolio_new_digital_art = isset( $_POST[ 'portfolio_digital_art' ] ) ? 'checked' : '';
        $portfolio_new_digital_art = isset( $_POST[ 'portfolio_graphic_design' ] ) ? 'checked' : '';

        // Update the meta field in the database.
        update_post_meta( $post_id, 'portfolio_web_design', $portfolio_new_web_design  );
        update_post_meta( $post_id, 'portfolio_web_dev', $portfolio_new_web_dev  );
        update_post_meta( $post_id, 'portfolio_digital_art', $portfolio_new_digital_art  );
        update_post_meta( $post_id, 'portfolio_graphic_design', $portfolio_new_digital_art  );
    }
}
new portfolio_metabox;
?>

答案 2 :(得分:0)

这应该这样做:

class portfolio_metabox {

    public function __construct() {

        if ( is_admin() ) {
            add_action( 'load-post.php', array( $this, 'init_metabox' ) );
            add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );
        }

    }

    public function init_metabox() {

        add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) );
        add_action( 'save_post', array( $this, 'save_metabox' ), 10, 2 );

    }

    public function add_metabox() {

        add_meta_box(
            'details',
            __( 'Project Details', 'chic' ),
            array( $this, 'render_project_metabox' ),
            'portfolio',
            'advanced',
            'default'
        );

    }

    public function render_project_metabox( $post ) {
        // Retrieve an existing value from the database.
        $post_meta = get_post_meta( $post->ID );

                $portfolio_web_design = ( isset($post_meta['portfolio_web_design'][0]) && $post_meta['portfolio_web_design'][0] != '' ) ? $post_meta['portfolio_web_design'][0] : 0;
                $portfolio_web_dev = ( isset($post_meta['portfolio_web_dev'][0]) && $post_meta['portfolio_web_dev'][0]!= '' ) ? $post_meta['portfolio_web_dev'][0] : 0;
                $portfolio_digital_art = ( isset($post_meta['portfolio_digital_art'][0]) && $post_meta['portfolio_digital_art'][0]!= '' ) ? $post_meta['portfolio_digital_art'][0] : 0;
                $portfolio_graphic_design = ( isset($post_meta['portfolio_graphic_design'][0]) && $post_meta['portfolio_graphic_design'][0]!= '' ) ? $post_meta['portfolio_graphic_design'][0] : 0;
        // Set default values.

        // Form fields.
        ?>
        <table class="form-table">
            <tr>
                <th><label for="portfolio_project" class="portfolio_project_label"><?php esc_html_e( 'Project Type', 'chic' ) ?></label;></th>
                <td>
                    <label><input type="checkbox" name="portfolio_web_design" class="portfolio_project_field" value="1" <?php checked( $portfolio_web_design , 1 ) ?>> <?php esc_html_e( ' Web Design', 'chic' ); ?></label><br>
                    <label><input type="checkbox" name="portfolio_web_dev" class="portfolio_project_field" value="1" <?php checked( $portfolio_web_dev , 1 ) ?>> <?php esc_html_e( ' Web Development', 'chic' ); ?></label><br>
                    <label><input type="checkbox" name="portfolio_digital_art" class="portfolio_project_field" value="1" <?php checked( $portfolio_digital_art , 1 ) ?>> <?php esc_html_e( ' Digital Art', 'chic' ); ?></label><br>
                    <label><input type="checkbox" name="portfolio_graphic_design" class="portfolio_project_field" value="1" <?php checked( $portfolio_graphic_design , 1 ) ?>> <?php esc_html_e( ' Graphic Design', 'chic' ); ?></label><br>
                    <p class="description"><?php esc_html_e( 'Project Type', 'chic' ) ?></p;>
                </td>
            </tr>
        </table>
        <?php
    }

    public function save_metabox( ) {
        global $post;
        // Sanitize user input.
        $portfolio_new_web_design = ( isset( $_POST['portfolio_web_design'] ) && $_POST['portfolio_web_design'] == 1 ) ? 1 : 0;
        $portfolio_new_web_dev = ( isset( $_POST['portfolio_web_dev'] ) && $_POST['portfolio_web_dev'] == 1 ) ? 1 : 0;
        $portfolio_new_digital_art = ( isset( $_POST['portfolio_digital_art'] ) && $_POST['portfolio_digital_art'] == 1 ) ? 1 : 0;
        $portfolio_new_digital_art = ( isset( $_POST['portfolio_graphic_design'] ) && $_POST['portfolio_graphic_design'] == 1 ) ? 1 : 0;

        // Update the meta field in the database.
        update_post_meta( $post->ID, 'portfolio_web_design', $portfolio_new_web_design );
        update_post_meta( $post->ID, 'portfolio_web_dev', $portfolio_new_web_dev );
        update_post_meta( $post->ID, 'portfolio_digital_art', $portfolio_new_digital_art );
        update_post_meta( $post->ID, 'portfolio_graphic_design', $portfolio_new_digital_art );

    }

}

new portfolio_metabox;

我清理了一下你的代码。尽量不要在一切之后放置空间,这是没有必要的。问题在于你将post meta拉出来的方式。像$portfolio_web_design这样的变量是空的。

我对此进行了测试,但确实有效。

希望这会有所帮助:)