Site icon Hip-Hop Website Design and Development

How can I show contents of a template part inside of another page?

WordPress is of course made up of templates and the template parts that make up those templates. I know I can override the template parts – that’s not what I’m trying to do here. I’m trying take the HTML generated by said template part and display it elsewhere.

How can I get the rendered HTML data from a template part for a given Page/Post ID and display it as part of another page, preferably as a shortcode? Here is an example.

Let’s say that post with ID = 20 has comments I’d like to display on a separate page. I want to write a shortcode that does this:

— locate comment data for the post with ID = 20

— apply that ID to the template part .//templates/comments.php and store the rendered HTML in a shortcode

— use my custom shortcode anywhere on my page to display it. Ultimately I would show the comments with a similar [comments id=”20″]

I know I’m using “comments” here but it’s just an example. This will have applications elsewhere, especially with WooCommerce for example where I want to show specific product attribute for a given product.