How To Pass Current Post Type ID from Single Template To Custom Page Template

admin2025-06-04  11

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)

Share Improve this question edited Dec 31, 2018 at 17:58 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Dec 31, 2018 at 15:25 Mona CoderMona Coder 4062 gold badges6 silver badges20 bronze badges 4
  • 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, movies to 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}.php right? No what I need to pass this endpoint Post ID from single-{post-type}.php to 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
Add a comment  | 

1 Answer 1

Reset to default 0

First 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749027075a315738.html

最新回复(0)