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