admin管理员组

文章数量:1122833

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.

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.

Share Improve this question edited Apr 30, 2018 at 14:22 Mark Kaplun 23.7k7 gold badges43 silver badges65 bronze badges asked Apr 30, 2018 at 14:13 user658182user658182 6152 gold badges14 silver badges34 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

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' );

When I am trying to do something like this, I will use ob_start() and ob_get_clean() to convert the resulting template part into a string that can be returned for the shortcode.

A common gotcha with shortcodes is if you ECHO the output it will output the content at the wrong time.

Next, you'll want to use include locate_template('template-name.php') so your variables are passed in the proper scope.

If you're trying to load in a template part directly, you can do something like this (this code is untested):

// [get_comment id=0]
function get_comment_shortcode($atts){
  $a = shortcode_atts([
    'id' => 0,
  ], $atts);

  if(!get_comment($a['id'])) return false; //Bail early if the comment doesn't exist
  $id = $a['id']; //reference this ID in your template to get the comment ID
  ob_start();
  include locate_template('comment-template.php');
  $result = ob_get_clean();
  return $result;
}

add_shortcode('get_comment', 'get_comment_shortcode');

References: https://codex.wordpress.org/Function_Reference/locate_template https://codex.wordpress.org/Shortcode_API

本文标签: How can I show contents of a template part inside of another page