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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4075064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:40:03  来源:igfitidea点击:

How to get post id from permalink (pretty url)?

wordpresspermalinks

提问by Jeaf Gilbert

How to get post id from permalink (pretty url)?

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

回答by kovshenin

You should be fine with url_to_postid()[see documentation] which is located in rewrite.php. I used it in a plugin of mine last year, works like a charm.

您应该可以使用位于 rewrite.php 中的url_to_postid()[参见文档]。我去年在我的一个插件中使用了它,效果很好。

回答by mems

I've got a dedicated (& documented) function for that:

我有一个专门的(和记录的)功能:

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

Retrieves a page given its path.

检索给定路径的页面。

Where $page_pathis

哪里$page_path

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

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

See Function Reference/get page by path

按路径查看函数参考/获取页面

Example:

例子:

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

回答by deweydb

Thisworks for regular post types AND custom post types. url_to_postid()only works for regular posts.

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

回答by jovaniwayne

url_to_postid()as of 3.7.0: This function now supports custom post types (see Trac tickets #19744, #25659).

url_to_postid()截至3.7.0:此功能现在支持自定义帖子类型(请参阅跟踪票证#19744#25659)。

回答by khandaniel

I have a multisite WP so once I go through the blogs for some reasons in some blogs url_to_postid()works, in other blogs on the post of the same type it doesn't while get_page_by_path()works like charm. So I made it this way, which may be not perfect though:

我有一个多站点 WP,所以一旦我出于某些原因浏览了一些博客中的博客url_to_postid(),在相同类型的其他博客中,它不会get_page_by_path()像魅力一样工作。所以我这样做了,虽然这可能并不完美:

$parsed_url = wp_parse_url( $url ); // Parse URL
$slug = substr($parsed_url['path'], 1 ); // Trim slash in the beginning

$post_id = url_to_postid( $slug ); // Attempt to get post ID

if ( ! $post_id ) { // If it didn't work try to get it manually from DB
    $post_query_result = 
        $wpdb->get_row("SELECT ID FROM {$wpdb->prefix}posts WHERE post_name = '{$slug}'");
    $analog_id = (int) $post_query_result->ID;
}

回答by jovaniwayne

you can try this one also:

你也可以试试这个:

$post = get_page_by_path('cat',OBJECT,'animal'); 

cat is the one you are looking for= the permalink; animal is the custom post type,

cat 是您正在寻找的那个=永久链接;动物是自定义帖子类型,

回答by Cybernob Technologies

please use

请用

  $postid = url_to_postid( $url );

to retrive the ID of an attachment.

检索附件的 ID。

It is required that the url provided be in the format of example.com/?attachment_id=Nand will not work with the full URL to get the id from the full URL.

要求提供的 url 格式为example.com/?attachment_id=N并且不能与完整 URL 一起使用以从完整 URL 获取 id。