我正在编辑帖子时向“精选图片”元数据添加选项。在metabox中我需要访问帖子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;
}
这些是重现我所看到的内容的步骤:
我的问题:如何在特色图像元数据框中始终获取正在编辑的页面的ID?我想尝试避免使用javascript。
如何在精选图像元数据框中始终获取正在编辑的页面的ID?
将function
设置为接受两个参数,如下所示:
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;
}
有关更多信息,请参阅https://developer.wordpress.org/reference/hooks/admin_post_thumbnail_html/。