我的元框更新帖子的wp_postmeta,但不更新页面的wp_postmeta。代码出了什么问题?

时间:2013-12-11 作者:Bojana Šekeljić

我在帖子上有一个简单的复选框,可以在滑块中显示帖子,一切正常。但我想切换并只在滑块中添加页面,但下面的代码并没有更新页面的wp\\U Posteta表,只更新帖子。你能告诉我我做错了什么吗

function damad_add_post_metabox() {

$screens = array( \'post\', \'page\');

foreach ( $screens as $screen ) {

    add_meta_box(
        \'damad_post_meta_id\',
        __( \'Options\', \'damad\' ),
        \'damad_inner_custom_box\',
        $screen
    );
}
}
add_action( \'add_meta_boxes\', \'damad_add_post_metabox\' );

function damad_inner_custom_box( $post ) {

wp_nonce_field( \'damad_inner_custom_box\', \'damad_inner_custom_box_nonce\' );

  $homepage = get_post_meta( $post->ID, \'_post_featured\', true);
  echo \'<label for="post-featured">\';
       _e("Include in homepage slider", \'damad\');
  echo "</label>";
  echo \'<input type="checkbox" id="post-featured" name="post-featured" value="post-featured"\';
  if ( \'post-featured\' == $homepage ) {
    echo \'checked="checked"\';
  } 
  echo \'/>\';
}

 function damad_save_postdata( $post_id ) {

  if ( ! isset( $_POST[\'damad_inner_custom_box_nonce\'] ) )
   return $post_id;

  $nonce = $_POST[\'damad_inner_custom_box_nonce\'];

  if ( ! wp_verify_nonce( $nonce, \'damad_inner_custom_box\' ) )
     return $post_id;

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

  if ( \'page\' == $_POST[\'post_type\'] ) {

  if ( ! current_user_can( \'edit_page\', $post_id ) )
    return $post_id;

 } else {

   if ( ! current_user_can( \'edit_post\', $post_id ) )
    return $post_id;
 }

$featureddata = $_POST[\'post-featured\'];

  update_post_meta( $post_id, \'_post_featured\', $featureddata );
}
add_action( \'save_post\', \'damad_save_postdata\' );

1 个回复
SO网友:Shazzad

更改线路

if ( \'page\' == $_POST[\'post_type\'] ) {

if ( \'page\' == get_post_type($post_id) ) {

结束

相关推荐

Image media upload metabox

我已经创建了第一个图像上载元数据库,但似乎有问题。当我添加图像然后更新页面时,数据不会保存-我确信这是我自己的错别字,我看不到。第二个输入框的上载图像按钮无法像第一个一样加载媒体库框-Js可能会出现问题吗?这是我的代码<?php /* Plugin Name: Meta Box Media Library Image Plugin URI: Description: Adds the ability to select an image from the