WordPress - 获取帖子内容中第一张图片的 ID

问题描述

作为一名新设计师,我不熟悉底层 wordpress 功能的使用。我在学习。我需要复制我们编写的帖子的组件,这需要我提取 WP 帖子对象的某些方面以供重新使用。我在理解“get_post”/“getattachment”和相关函数的使用方面取得了进展,但在尝试提取帖子内容第一张图片的 image_id 几天后我被卡住了。例如,当我运行此代码段时

        $data = get_posts( array(
       'post_type'   => 'post','numberposts' => -1,'post_status' => null
       
    ) );

我得到了充满帖子数据的数组。以下数组中一篇文章的摘录

[2]=>
object(WP_Post)#4039 (24) {
["ID"]=>
int(3013)
["post_author"]=>
string(1) "1"
["post_date"]=>
string(19) "2021-01-16 07:14:08"
["post_date_gmt"]=>
string(19) "2021-01-16 07:14:08"
["post_content"]=>
string(1367) "


        FirsT IMAGE FROM MEDIA (How do i get its ID or URL for re-use)
        (PLUS SOME WORDS)

"
["post_title"]=>
string(16) "1.Another Star"
["post_excerpt"]=>
string(0) ""
["post_status"]=>
string(7) "publish"
["comment_status"]=>
string(4) "open"
["ping_status"]=>
string(4) "open"
["post_password"]=>
string(0) ""
["post_name"]=>
string(16) "1-another-star"
["to_ping"]=>
string(0) ""
["pinged"]=>
string(0) ""
["post_modified"]=>
string(19) "2021-01-17 19:32:36"
["post_modified_gmt"]=>
string(19) "2021-01-17 19:32:36"
["post_content_filtered"]=>
string(0) ""
["post_parent"]=>
int(0)
["guid"]=>
string(26) "http://example.com/?p=3013"
["menu_order"]=>
int(0)
["post_type"]=>
string(4) "post"
["post_mime_type"]=>
string(0) ""
["comment_count"]=>
string(1) "0"
["filter"]=>
string(3) "raw"
}       

我的问题是如何以编程方式获取“发布内容”中呈现的图像的 ID,以便我可以在其他地方使用它。我试过运行像

这样的查询
     $attachments = get_posts( array(
     'post_type'   => 'attachment','post_status' => null
    ) );

它为我提供了媒体文件夹中附件的数据,但这并没有说明媒体也附加了哪些帖子。所以我从两个角度来试图从 post_id 中获得一些给我一个 image_id 的东西。反之亦然。

我确定这完全取决于我对这些东西的新手身材。所以真的很感激任何转向。谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)