保存帖子不会在页面上输出任何内容

时间:2014-12-17 01:43:58

标签: php wordpress

下面是用于在WordPress上创建自定义元数据的代码,Metabox显示正常,但是当我保存帖子时它不会将值转储到页面中。它应该转储此函数“ product_meta_box_save ”中的值,这会告诉WordPress在页面保存时触发。

<?php
// Little function to return a custom field value
function product_get_custom_field( $value ) {
    global $post;
}

// Register the Metabox
function product_add_custom_meta_box() {
    add_meta_box( 'about-products-', __( 'About the Product'), 'product_meta_box_output', 'products', 'normal', 'high' );
}
add_action( 'add_meta_boxes', 'product_add_custom_meta_box' );

// Output the Metabox
function product_meta_box_output( $post ){
?>
  <table width="100%">
    <tr>
      <td width="18%"><?php _e("Product Price (FJD)"); ?></td>
      <td width="82%"><input type="text" size="20" name="product_price"/></td>
    </tr>
    <tr>
      <td><?php _e("Product Stock"); ?></td>
      <td><input type="number" size="50" name="product_stock"/></td>
    </tr>
  </table>
<?php }

// Save the Metabox values
function product_meta_box_save( $post_id ) {
    global $post;
    var_dump( $post );
}
add_action( 'save_post', 'product_meta_box_save' );

1 个答案:

答案 0 :(得分:1)

是的,因为大卫说它使用ajax来保存帖子,所以它不会向你显示这些数据。

相反,您应该使用保存功能执行类似的操作:

function product_meta_box_save( $post_id ) 
{
       if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )  
       {
           return; //this prevents it from saving the values during its autosaves
       }

       if ( $_POST && isset($_POST['metabox_data'] ) ) 
       {
           update_post_meta($post_id, 'metabox_data', $_POST['metabox_data'] );
       }
}

通过这种方式,数据将会保存,正如大卫所说,你可以将其丢弃,然后随意做任何事情。