如何从永久链接获取帖子ID(漂亮的网址)?

Jea*_*ert 38 wordpress permalinks

如何从永久链接获取帖子ID(漂亮的网址)?

kov*_*nin 52

您应该没有url_to_postid()[ 见文档 ],它位于rewrite.php中.我去年在我的插件中使用它,就像一个魅力.

  • 好.现在至少记录在案.http://codex.wordpress.org/Function_Reference/url_to_postid (6认同)
  • 您还应该注意文档提到:"请注意,这不会返回自定义帖子类型的帖子ID." (4认同)
  • 从WordPress 3.7.0开始,还支持自定义帖子类型.有关详细信息,请参阅上面的链 (4认同)

mem*_*ems 9

我有一个专门的(和文档)功能:

get_page_by_path( $page_path, $output, $post_type );
Run Code Online (Sandbox Code Playgroud)

检索给定其路径的页面.

哪里$page_path

[...]相当于'pagename'查询,如:'index.php?pagename = parent-page/sub-page'.

请参阅函数参考/按路径获取页面

例:

// Assume 'my_permalink' is a post.
// But all types are supported: post, page, attachment, custom post type, etc.
// See http://codex.wordpress.org/Post_Types
get_page_by_path('my_permalink', OBJECT, 'post');
Run Code Online (Sandbox Code Playgroud)


dew*_*ydb 9

适用于常规帖子类型和自定义帖子类型.url_to_postid()仅适用于常规帖子.