Use setup_postdata()
to make the ‘current post’ any given post, then retrieve the template part before resetting post data back to the original post. Any template tags that depend on the current post in The Loop that are in the template part will refer to the post you have set up with that function.
function wpse_302305_shortcode( $atts ) {
global $post;
$atts = shortcode_atts( ['id' => 0], $atts );
$post = get_post( $atts['id'] );
ob_start();
if ( $post ) {
setup_postdata( $post );
get_template_part( 'path/to/template' );
wp_reset_postdata();
}
return ob_get_clean();
}
add_shortcode( 'template', 'wpse_302305_shortcode' );
Related Posts:
- Is there any way to use get_template_part() with folders?
- Is it possible to override the result of get_template_part()?
- Get template part vs locate template function
- Templates & CSS – Proper Programming Practice?
- TwentyTen: Overloading template.php files vs. get_template_part
- Passing variables to template parts
- How to move style from template file to section?
- Unable to include a template
- Where is the template for the ‘standard’ format of a post in the theme twentythirteen?
- Valid HTML in Template Part
- Remove portion of header code from home page only
- Use a separate template for the home page only
- How to create new template page and template-parts in wordpress theme [closed]
- Including template pages within another template?
- Organize template parts and page templates in folders in regards of template hierarchy
- How to get all template files used for rendering a single post/page?
- get_template_part for template in subdirectory not working
- Display a custom 404 page without a redirect
- When to use content-pagename.php?
- Is it possible to call a template file inside wysiwyg editor? And how?
- How can i change email template for new user
- Enable page templates. How?
- What’s the purpose of the paged.php file?
- Prevent comments_template() to load comments.php
- Page editor missing Templates drop down
- Custom templates folder
- What is the link to my default “archive” page?
- How to Rename a Template File?
- get page templates
- How do you check if a WordPress template file exist?
- How can I see what template parts are being called for rendering the viewable page?
- How to hide/redirect the author page
- Gutenberg & Pre-formatted Templates: Core Block Attributes
- How to assign a class to a page with a custom template?
- Proper Javascript Implementation
- Guest author’s post not display in author template
- How to apply a custom template for posts by default?
- Is there a way to check which template file is being loaded, if it is not a page template file?
- How to remove wp_link_pages() from template
- How can I get WP to use templates in lower-level subfolder?
- how to make a home page for wordpress custom theme
- Generate Catalog Of Posts Based On Template
- Extending body classes in front- and backend
- How do I find the directory of a page template?
- Is there a way to set different post templates for parent posts and child posts in the same post type?
- Why write markup for index.php?
- LaTeX for WordPress strips codes in loop
- Having trouble with Template hierarchy. I Need to create a set of pages that drill down from states to specific locations
- How can I hardcode template selection?
- Changing layout programmatically
- How to identify which template WordPress’s default search-form retrieves in my theme
- Unable to display pagination links on custom category template
- How to use same page template if post status other than ‘publish’?
- Where is Number of Posts Set?
- How can I force URL of a custom post type archive to use a page template?
- How do I override the search template in a plugin?
- How do I make a variable available inside partials?
- Is there any way to write custom HTML is some .php file and then call it wherever necessary?
- How to redirect my custom template page to content-none.php if no posts found?
- Are there any downsides to not using get_header(), get_sidebar and get_footer()?
- Get URL of current post but not the page number
- Is template name always same as directory name?
- Single Post Breadcrumb not linking back to Custom Post Type Index?
- Form post to WordPress page = wrong template
- get_month_link uses what wordpress template?
- Load custom script on a single template
- Archive template limiting to 4 entries?
- Author template help. How to check if field exists in the profile?
- Can you use block patterns in block templates or insert them programmatically?
- WP redirect to custom login page if a user is not logged in
- Change default template in the block editor (Gutenberg)
- How do I create post template for different taxonomy
- Ip2location plugin in my template header?
- Import a header template from another theme
- Will placing custom templates in sub directory break any native WordPress functionality
- Adding text to the footer of each post based on tags or categories
- Archive-custome_post.php template not working
- Change “posts per page” depending on width
- How to slice down index.php from a template and import from another file?
- Can index.php take over for front-page.php in template hierarchy on second page?
- What filter or action hook to use in order to load some code before the template begins printing in BuddyPress? [closed]
- Make Permalink go to specific template
- WordPress function.php on ZendFramework CMS
- How do I get tags linking to proper pages (/tag/sample-tag -> pages with that tag)
- How to name custom search page PHP file?
- Page Template Selector Error
- Assign template to custom page type?
- Accidentally deleted code in header.php
- Custom post template
- I want to display all related posts for a selected tag in WordPress
- how to change custom post type search template to output search results in posttype-archive.php
- WordPress Child Theme Template auth_redirect wrapper
- Show a custom template, no matter the page being viewed, if the user is not logged in
- printf, translation and the_author_posts_link()
- Search results in custom template
- Tag Posts showing in wrong places
- front page won’t change templates
- Including 2 Negative Is_Template Conditionals in header.php [closed]
- Add custom card template to masonry layout using Essential Grid [closed]
- Where is the general html template file?