问题描述

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

最佳解决方法

您应该在位于 rewrite.php 中的 url_to_postid() [see documentation] 。我去年在我的一个插件中使用它,像一个魅力一样。

次佳解决方法

This 适用于常规帖子类型和自定义帖子类型。 url_to_postid()仅适用于常规职位。

第三种解决方法

我有一个专门的 (&文件) 功能:

get_page_by_path( $page_path, $output, $post_type );

Retrieves a page given its path.

$page_path 是哪里

[…] the equivalent of the ‘pagename’ query, as in: ‘index.php?pagename=parent-page/sub-page’.

Function Reference/get page by path

例:

// 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');

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。