問題描述

如何從永久鏈接 (漂亮的 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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。