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.com/%postname%/
, then you can just add the pages as sub pages of your posts. More details here.
Related Posts:
- Custom post type not respecting template hierarchy
- What is singular.php?
- Is it possible to use a single custom post as the site front page
- Template for specific post of custom post type
- Listing a post’s categories and subcategories
- Single post comment template not working
- How do I show google ads between post content?
- Add inline style to get_the_category_list
- Help with figuring out the future post workaround
- Disable single view for specific post category
- Only nextpagelink on wp_link_pages
- Display a post map on a blank/new page
- Is it possible to create rel=”noindex, nofollow” for a single post if contain bad keywords?
- the_excerpt() in content.php and get_template_part() in single.php
- Custom seperate Single.php
- Set a cookie only in single posts
- single.php with different look by category
- diffrent style for post single page
- WordPress object for comments frame [closed]
- I’m having a problem viewing the Youtube video
- Use different «Single.php» for custom categories
- force https except one page/post
- Single custom post type template not working (single-custom_post_template.php) not working
- Single.php category entries not showing right colours
- Link from archive-page.php to single-page.php
- How to check if a post is in categories x,y,z inludint their subcategories
- Changing the display of the themename_posted_on() function
- How to Highlight current category on category and single page template?
- Exclude single page from function
- WordPress, alternative single post template
- get full real path of thumbnail post img wordpress
- how to loop through this in blog single?
- Modify post template on Divi Theme
- I want to signin from first wordpress site to other wordpress website without registration..without using network mode
- Try to showing product file size on product single page
- Improve related posts
- Page Templates Used in Custom Post Type
- Get active term on single (CPT) page
- Having single posts appear under the blog homepage and highlight menu items properly?
- Single.php loading fully on localhost but not on server
- explain why is_single doesn’t work
- How to check if the product is in a certain category on a single-product.php in Woocommerce?
- CPT Template Not Showing – Getting 404
- Pre get posts for single post
- Pull Two Posts Into Custom Post Type `single-cpt.php`
- is_singular() not working if called via callback function of admin-ajax.php
- LOST in WP Template Hireachy
- Proper way to load a single post via Ajax?
- How to Handle CSS for Multiple Header header.php Files?
- Static Front Page problem
- Problem getting single_template filter to work – I want to serve a different single.php file for posts in a certain category
- Overriding default template if page slug and post type are same
- Displaying Meta Box Image
- Trying to manage templates on a blog with lots of custom taxonomies
- is_archive() content being shown instead of is_category()
- custom single.php not working
- How to apply a custom template for posts by default?
- Which php file lists all the post of a category
- Displaying all Video Post Formats to Page
- Custom post type and taxonomy permalinks – Structure
- WP Query Category Atribute Not Working
- Manipulating show_on_front, page_on_front, page_for_posts and template hierarchy
- Is there a way to set different post templates for parent posts and child posts in the same post type?
- changing permalink for custom template in posts of a certain category
- Display all posts that were published before full post on single.php
- How to load parent theme template parts in child theme
- Selected template is not respected for home page in WordPress
- How does WordPress choose archive type template?
- Insert sometext after first h3 in content
- Will renaming a `content.php` file cause any problems?
- how to force tag page layout to use same as search layout?
- Cannot retieve the_content() and the_author() – both returning empty strings
- Including all post id’s of a custom post type into an array
- Is it possible to customize the post according to post format in single.php?
- get_term_link is Ending to single.php
- Post type no single page
- Differentiation between index.php and page.php
- Single-page.php template file name not shown
- How to display custom post type?
- adding custom post type “name” to single template
- loading custom registration template
- Custom post types templates
- How to Override Page Template if URL matches query?
- My single.php page skips the first div tag
- Overriding the template files using a plugin for all themes
- Static page won’t identify template
- Add custom field information to source meta data
- display multiple posts and posts content on a single URL
- How to adjust Related Page Thumb Sizes on single page?
- When category name and title are the same … WP will select archive.php instead of single.php
- Single Page of Custom Post Type Not Showing the Content Of post
- Custom Post Type Archive – archive.php is present, but it is not being used
- How to Fix Custom WordPress Loop with Pagination on Single Post View?
- Most efficient way to have 1 template for parent, 1 template for child categories?
- Post Thumbnail on Single (if elseif else)
- Parent for a single (single.php)
- How do I get a single page navigation depending on the previous page?
- Wrap div-tag around posts in page
- How to get the featured image inside of a single-.php template?
- What is the point of using archive.php instead of index.php?