Wordpress 管理员:post 特色图片元数据框中的 ID 更改
Wordpress admin: post ID changes in featured image metabox
我在编辑 post 时向 "Featured image" 元数据框添加选项。我需要从 metabox 中访问 post ID。这在 post.php 首次加载时工作正常。但是,如果我 "Select featured image" 或 "Remove featured image",当元数据框重新加载时,post ID 会更改(变为静态主页 ID)。
以下代码将在特色图片框中显示 post 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;
}
这些是重现我所见内容的步骤:
- 编辑一个post
- 请注意 post ID 是正确的(例如 7)
- 单击 "Select featured image" 和 select 图片
- metabox 刷新以显示 select 图像
- 请注意post ID现在不正确(静态首页的ID)
我的问题:如何始终从精选图像元数据框中获取正在编辑的页面的 ID?我想尝试避免 javascript.
How can I consistently get the ID of the page being edited from within
the featured image metabox?
设置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/。
我在编辑 post 时向 "Featured image" 元数据框添加选项。我需要从 metabox 中访问 post ID。这在 post.php 首次加载时工作正常。但是,如果我 "Select featured image" 或 "Remove featured image",当元数据框重新加载时,post ID 会更改(变为静态主页 ID)。
以下代码将在特色图片框中显示 post 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;
}
这些是重现我所见内容的步骤:
- 编辑一个post
- 请注意 post ID 是正确的(例如 7)
- 单击 "Select featured image" 和 select 图片
- metabox 刷新以显示 select 图像
- 请注意post ID现在不正确(静态首页的ID)
我的问题:如何始终从精选图像元数据框中获取正在编辑的页面的 ID?我想尝试避免 javascript.
How can I consistently get the ID of the page being edited from within the featured image metabox?
设置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/。