admin管理员组文章数量:1130349
Having a Custoom Post Type called movies, I am able to get the current Post ID in my single-movies.php template. Now can you please let me know how I can pass this post ID from the single template to a custom page template like page-postDetail.php basically what I want to do is adding some more details to the post in the custom page (Like having multi single template foe one custom post type)
Having a Custoom Post Type called movies, I am able to get the current Post ID in my single-movies.php template. Now can you please let me know how I can pass this post ID from the single template to a custom page template like page-postDetail.php basically what I want to do is adding some more details to the post in the custom page (Like having multi single template foe one custom post type)
1 Answer
Reset to default 0First of all, (you probably know this but..) you are fighting how WP is intended to work. You can not get the post ID directly in your custom page template because it is not displaying the post. So you will have to, as you said, send the post ID to the page template.
The best way I can think of, to do it this way, is to send it to the new page via GET or POST. The most simple way would be to add a query string to whatever link goes to the sub-page containing the ID, which might look like this ?post_id=123. Then, you can parse the URL query string to get the ID and use it as needed.
Alternatively, you could make your CPT (Custom Post Type) structure hierarchical and set your permalink structure to example/%postname%/, then you can just add the pages as sub pages of your posts. More details here.
Having a Custoom Post Type called movies, I am able to get the current Post ID in my single-movies.php template. Now can you please let me know how I can pass this post ID from the single template to a custom page template like page-postDetail.php basically what I want to do is adding some more details to the post in the custom page (Like having multi single template foe one custom post type)
Having a Custoom Post Type called movies, I am able to get the current Post ID in my single-movies.php template. Now can you please let me know how I can pass this post ID from the single template to a custom page template like page-postDetail.php basically what I want to do is adding some more details to the post in the custom page (Like having multi single template foe one custom post type)
-
If you want a custom page template to be available for a for a custom post type you can do this in 4.7+ by adding
Template Post Type: page, moviesto the template header: developer.wordpress/themes/template-files-section/… – Jacob Peattie Commented Dec 31, 2018 at 15:36 - How are you currently acquiring the post ID? And can you not do the same thing in the other template? – Tom J Nowell ♦ Commented Dec 31, 2018 at 15:54
-
Thanks guys for commenting out. @JacobPeattie, This is not what I am looking for. What I need is a page which getting Post ID of last viewed single template. @Tom, lets say users clicked a Movie from all custom Post type template this will automatically land in the
single-{post-type}.phpright? No what I need to pass this endpoint Post ID fromsingle-{post-type}.phpto another page. As you know the other page has no idea what is the last viewed single post – Mona Coder Commented Dec 31, 2018 at 16:22 -
What about using a rewrite endpoint? You would have a URL like
/movie/postname/details/for each post. – Milo Commented Jan 1, 2019 at 4:56
1 Answer
Reset to default 0First of all, (you probably know this but..) you are fighting how WP is intended to work. You can not get the post ID directly in your custom page template because it is not displaying the post. So you will have to, as you said, send the post ID to the page template.
The best way I can think of, to do it this way, is to send it to the new page via GET or POST. The most simple way would be to add a query string to whatever link goes to the sub-page containing the ID, which might look like this ?post_id=123. Then, you can parse the URL query string to get the ID and use it as needed.
Alternatively, you could make your CPT (Custom Post Type) structure hierarchical and set your permalink structure to example/%postname%/, then you can just add the pages as sub pages of your posts. More details here.
本文标签: How To Pass Current Post Type ID from Single Template To Custom Page Template
版权声明:本文标题:How To Pass Current Post Type ID from Single Template To Custom Page Template 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749027051a2305268.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


Template Post Type: page, moviesto the template header: developer.wordpress/themes/template-files-section/… – Jacob Peattie Commented Dec 31, 2018 at 15:36single-{post-type}.phpright? No what I need to pass this endpoint Post ID fromsingle-{post-type}.phpto another page. As you know the other page has no idea what is the last viewed single post – Mona Coder Commented Dec 31, 2018 at 16:22/movie/postname/details/for each post. – Milo Commented Jan 1, 2019 at 4:56