最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - post_type internal ID?

programmeradmin5浏览0评论

This may sound like a stupid question, but is there something like an ID associated with the post types (built in or custom) in WordPress?

I'm not interested in the actual post id, but wondering if there's something like this:

ID         Post type slug
-------------------------
0          post
1          page
2          slider
3          section
...

What I would need would be something like get_post_type_id( $post_type_slug ) so that when I call get_post_type_id( 'slider' ) it would return 2.

The reason: I'm building a post slider shortcode and I need a unique ID that could be in the form of #{$post_type}-slider-{$id}, to avoid duplicates and correctly apply JavaScript.

Thanks!

This may sound like a stupid question, but is there something like an ID associated with the post types (built in or custom) in WordPress?

I'm not interested in the actual post id, but wondering if there's something like this:

ID         Post type slug
-------------------------
0          post
1          page
2          slider
3          section
...

What I would need would be something like get_post_type_id( $post_type_slug ) so that when I call get_post_type_id( 'slider' ) it would return 2.

The reason: I'm building a post slider shortcode and I need a unique ID that could be in the form of #{$post_type}-slider-{$id}, to avoid duplicates and correctly apply JavaScript.

Thanks!

Share Improve this question asked Oct 6, 2020 at 9:31 GeorgeGeorge 33 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

No there isn't, and it isn't necessary. Post type names are their own unique identifiers.

The primary reason there are no numeric IDs is because post types are registered on every page load in PHP, no table exists in the database for them, which means no row IDs. So there is no use for a unique ID that the post name doesn't already accomplish.

For this reason, you're perfectly fine to use the post types name in your ID attributes, post type names can't clash.

The same is true of taxonomy names, 2 taxonomies cannot share the same name.

发布评论

评论列表(0)

  1. 暂无评论