我的元盒不想节省价值

时间:2017-09-03 作者:Marcin Panek

我已经为此奋斗了三天,我不明白为什么我的代码不能工作?

<?php
    function my_custom_post_product() {
    $labels = array(
        \'name\'               => _x( \'Produkty\', \'post type general name\' ),
        \'singular_name\'      => _x( \'Produkt\', \'post type singular name\' ),
        \'add_new\'            => _x( \'Add New\', \'book\' ),
        \'add_new_item\'       => __( \'Add New Product\' ),
        \'edit_item\'          => __( \'Edit Product\' ),
        \'new_item\'           => __( \'New Product\' ),
        \'all_items\'          => __( \'All Products\' ),
        \'view_item\'          => __( \'View Product\' ),
        \'search_items\'       => __( \'Search Products\' ),
        \'not_found\'          => __( \'No products found\' ),
        \'not_found_in_trash\' => __( \'No products found in the Trash\' ), 
        \'parent_item_colon\'  => \'\',
        \'menu_name\'          => \'Products\'
    );
    $args = array(
        \'labels\'        => $labels,
        \'description\'   => \'Holds our products and product specific data\',
        \'public\'        => true,
        \'publicly_queryable\' => true,
        \'menu_position\' => 5,
        \'supports\'      => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\' ),
        \'has_archive\'   => true,
    );
    register_post_type( \'product\', $args ); 
    flush_rewrite_rules();
}
add_action( \'init\', \'my_custom_post_product\' );
function my_taxonomies_product() {
    $labels = array(
        \'name\'              => _x( \'Product Categories\', \'taxonomy general name\' ),
        \'singular_name\'     => _x( \'Product Category\', \'taxonomy singular name\' ),
        \'search_items\'      => __( \'Search Product Categories\' ),
        \'all_items\'         => __( \'All Product Categories\' ),
        \'parent_item\'       => __( \'Parent Product Category\' ),
        \'parent_item_colon\' => __( \'Parent Product Category:\' ),
        \'edit_item\'         => __( \'Edit Product Category\' ), 
        \'update_item\'       => __( \'Update Product Category\' ),
        \'add_new_item\'      => __( \'Add New Product Category\' ),
        \'new_item_name\'     => __( \'New Product Category\' ),
        \'menu_name\'         => __( \'Product Categories\' ),
    );
    $args = array(
        \'labels\' => $labels,
        \'hierarchical\' => true,
    );
    register_taxonomy( \'product_category\', \'product\', $args );
}
add_action( \'init\', \'my_taxonomies_product\', 0 );
add_action( \'add_meta_boxes\', \'product_price_box\' );
function product_price_box() {
    add_meta_box( 
        \'product_price_box\',
        __( \'Product Price\'),
        \'product_price_box_content_callback\',
        \'product\',
        \'normal\',
        \'high\'
    );
}

function product_price_box_content_callback( $post ) {
    wp_nonce_field( \'product_price_box_save\' , \'product_price_box_content_nonce\' );
    $value = get_post_meta($post->ID,  _product_price_value_key, true);
    echo \'<label for="product_price_field"></label>\';
    echo \'<input type="text" id="product_price_field" name="product_price_field" 
    placeholder="enter a price" value="\'. esc_attr( $value ) .\'" />\';
}

function product_price_box_save ( $post_id ) {
    if ( !isset( $_POST[\'product_price_box_content_nonce\'])) {
        return;
    }
    if ( !wp_verify_nonce($_POST[\'product_price_box_content_nonce\'], \'product_price_box_save\')) {
        return;
    }
    if( defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE ){
        return;
    }
    if ( !current_user_can(\'edit_post\', $post_id) ) {
        return;
    }
    if ( !isset($_POST[\'product_price_field\']) ) {
        return;
    }
    $save_data = sanitize_text_field( $_POST[\'product_price_field\'] );
    update_post_meta($post_id, \'_product_price_value_key\', $save_data);
}
add_action(\'save_post\', \'product_price_box_save\');

1 个回复
SO网友:Marcin Panek

我的代码工作得很好。值的第二个参数是字符串$value=get\\u post\\u meta($post->ID,“\\u product\\u price\\u value\\u key”,true);

结束

相关推荐

当限制为特定CPT时,自定义Metabox字段不保存

我有一系列的CPT,每个都有自己的自定义元数据库。它们都工作得很好——在应该展示的地方展示,省去了应该怎样做。但我发现的一个问题是,他们也都在每个管理页面上运行他们的查询(下拉菜单等),而且有很多。我将metabox选项的代码部分包装在条件代码中:function set_options() { parent::set_options(); global $pagenow; if ( \'post-new.php\' == $pagenow || \'post.php\' == $