Wordpress管理员:在特色图像元框中发布ID更改

Wordpress管理员:在特色图像元框中发布ID更改,wordpress,wordpress-admin,Wordpress,Wordpress Admin,我在编辑帖子时向“特色图片”元框添加选项。在metabox中,我需要访问post ID。当post.php首次加载时,这种方法可以正常工作。但是,如果我选择“选择特色图像”或“删除特色图像”,则在元数据库重新加载时,帖子ID会更改(变为静态主页ID) 以下是一些代码,将在特色图像框中显示帖子ID: add_filter( 'admin_post_thumbnail_html', 'add_options_to_featured_image' ); function add_options_to_

我在编辑帖子时向“特色图片”元框添加选项。在metabox中,我需要访问post ID。当post.php首次加载时,这种方法可以正常工作。但是,如果我选择“选择特色图像”或“删除特色图像”,则在元数据库重新加载时,帖子ID会更改(变为静态主页ID)

以下是一些代码,将在特色图像框中显示帖子ID:

add_filter( 'admin_post_thumbnail_html', 'add_options_to_featured_image' );
function add_options_to_featured_image( $html ){
    global $post;

    $html .= '<label>Post '.$post->ID.'</label>';

    return $html;
}
add_filter('admin_post_缩略图_html','add_options_至_特色图片');
函数将选项添加到图片($html){
全球$员额;
$html.='Post'.$Post->ID';
返回$html;
}
以下是重现我所见的步骤:

  • 编辑帖子
  • 请注意,post ID是正确的(例如7)
  • 单击“选择特色图像”并选择图像
  • 元盒将刷新以显示所选图像
  • 请注意,post ID现在不正确(静态首页的ID)
  • 我的问题是:如何从特色图像元盒中始终获取正在编辑的页面的ID?我想尽量避免使用javascript

    如何从内部一致地获取正在编辑的页面的ID 特色图像元盒

    函数设置为接受两个参数,如下所示:

    add_filter( 'admin_post_thumbnail_html', 'add_options_to_featured_image', 10, 2 );
    function add_options_to_featured_image( $html, $post_id ){
        $html .= '<label>Post '.$post_id.'</label>';
    
        return $html;
    }
    
    add_filter('admin_post_缩略图_html','add_options_至_特色图片',10,2);
    函数将选项添加到图片($html,$post\u id){
    $html.='Post'.$Post_id';
    返回$html;
    }
    
    有关更多信息,请参阅