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


Save Post doesn't output anything on page

下面是我用来在WordPress上创建自定义元框的代码,元框显示正常,但是当我保存帖子时,它不会将值转储到页面中。它应该从这个函数"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' );

是的,因为大卫说它使用 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'] );
       }
}

这样数据就会保存,正如大卫所说,你可以把它转储出来,用它做任何你想做的事情。