首页 > 解决方案 > 根据帖子链接获取帖子ID

问题描述

我想将 Wordpress 帖子链接转换为帖子 ID。例如,我有一个带有永久链接的帖子

site.com/the-first-post

现在我想要这个帖子ID。是否有这样的函数:输出帖子 ID 的 Sample_function("site.com/the-first-post")?

标签: wordpress

解决方案


Wordpress 具有内置功能,用于在给定其 url 的情况下检索页面/帖子的 id,或在失败时为零。可以认为是 get_permalink() 的反函数。

url_to_postid( site_url('the_slug') ); //the-first-post

您将在此处找到详细信息: https ://codex.wordpress.org/Function_Reference/url_to_postid


推荐阅读