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 disable the single view for a custom post type?
- Template Hierarchy: confused with index.php, front-page.php, home.php
- How to check if the product is in a certain category on a single-product.php in Woocommerce?
- How to create custom 401, 403 and 500 error pages?
- Custom post type single page returns 404 error
- single-{$post_type}-{slug}.php for custom post types
- Custom post type single-{custom}.php not working
- How to detect single.php (but not single-portfolio.php)?
- Conditional for single-{post-type}.php
- Custom Post Type Plugin: Where Do I Put The Template?
- Check IF is a “single product page” and Check the “role” for a Redirect
- How to override WooCommerce template files?
- How to modify single.php in a child theme?
- Custom taxonomy query broken after upgrade to 4.4
- CPT Template Not Showing – Getting 404
- Add_action to wp_head via functions.php
- Custom Single Post By Category
- Create “File-less” Page Template in Functions.php
- Overide get_template_part( ‘partials/post’, ‘sidebar’ ); with a plugin
- How to make child categories recognize parent’s template displays
- Using single.php from plugin folder instead of default template folder
- How do I get the category URL from get_the_category?
- Is it possible to manipulate the list of page templates?
- Custom permalink structure only for default posts
- Related Posts by Multiple Tags?
- How to use a specific category archive index as the site front page?
- Loading partial templates with AJAX/PJAX
- Is there a default template file for child pages / subpages?
- Different template for subcategories
- Single custom post type page redirecting to 404 page
- Multiple Single Post templates
- Taxonomy Templates… by hierarchical level?
- A special single page templates for posts under a category and all its child category
- How do I create a link that will always show the latest post?
- Posts in multiple Categories different single.php
- Display Editable Text Above CPT Archive Listings
- Pre get posts for single post
- Pull Two Posts Into Custom Post Type `single-cpt.php`
- Custom SQL query to get List of posts with featured image url
- single page wordpress
- Get taxonomy name of current post
- is_singular() not working if called via callback function of admin-ajax.php
- Different templates for parent and children categories/taxonomies
- Disable Single Post View for Specific Taxonomy on Custom Post Type
- Archive page showing nothing – issue with WordPress Hierarchy?
- How to use same theme template for multiple taxonomy terms?
- the_post_thumbnail() based on the Post ID
- How can I set up a category “overview” page?
- Is it possible to have an index page for taxonomy term for each custom post type it is assigned to?
- How do I create a widget that only allows a single instance?
- Custom Post Type with Nested Taxonomy and Template Files
- How to get a list of term names of the custom post type im currently on in single.php
- Post X of Y in single.php / sidebar.php
- HTML code in Custom field
- Which template name to use for taxonomies but not their terms?
- Store Page Template Files in a Subfolder
- is_singular won’t call my functions?
- LOST in WP Template Hireachy
- Templates & CSS – Proper Programming Practice?