【问题标题】:How to get the attachment id from the attachment page url?如何从附件页面 url 获取附件 ID?
【发布时间】:2021-10-13 08:02:50
【问题描述】:

我正在尝试从附件页面 url 获取附件的 ID。

创建自定义帖子时,我将媒体上传到帖子。

媒体现在有一个附件页面 - http://localhost/site/custom-post/post-name/screenshot_20211005-091742/

如何从该网址获取附件 ID?我已经尝试过attachment_url_to_postid 功能,但这似乎只适用于直接媒体网址,例如http://localhost/site/wp-content/uploads/2021/10/Screenshot_20211005-091742.png

我还测试了直接上传到媒体库的媒体 http://localhost/site/wp-content/uploads/2021/10/S036662860_0.jpg 但attachment_url_to_postid 返回 0。

【问题讨论】:

    标签: php wordpress wordpress-media


    【解决方案1】:

    您可以使用url_to_postid 代替attachment_url_to_postid,localhost/site/custom-post/post-name/screenshot_20211005-091742/ 是页面 URL,而不是直接文件 URL。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-26
      • 2010-11-11
      • 1970-01-01
      • 1970-01-01
      • 2011-10-11
      • 2016-03-22
      • 2020-06-01
      • 1970-01-01
      相关资源
      最近更新 更多