Wordpress管理员:在精选图片元数据中发布ID更改

问题描述 投票:0回答:1

我正在编辑帖子时向“精选图片”元数据添加选项。在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;
}

这些是重现我所看到的内容的步骤:

  1. 编辑帖子
  2. 请注意,帖子ID是正确的(例如7)
  3. 单击“选择特色图像”并选择图像
  4. 元数据库刷新以显示选择的图像
  5. 请注意,帖子ID现在不正确(静态首页的ID)

我的问题:如何在特色图像元数据框中始终获取正在编辑的页面的ID?我想尝试避免使用javascript。

wordpress wordpress-admin
1个回答
1
投票

如何在精选图像元数据框中始终获取正在编辑的页面的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/

© www.soinside.com 2019 - 2024. All rights reserved.