-3

I should create a shortcode to display the contents of the post in my template.

Could someone help me? I made a shortcode to display the form of comments, this just can not unfortunately.

Thanks again

closed as unclear what you're asking by Michael, TheDeadMedic, Howdy_McGee, birgire, Pieter Goosen Mar 8 '16 at 10:13

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Please see How to Ask and learn how to ask a question, then come back, file an edit – Pieter Goosen Feb 28 '16 at 14:04
  • I made a shortcode to display the form of comments. I guess you think we are wizards ;-) Where is your code, without your code we cannot help you – Pieter Goosen Feb 28 '16 at 14:40
0

Here is an example of a shortcode that renders post content based on the id and type attribute.

[post type="content" id="2"]

[post type="title" id="2"]

If you want to render from a template you would use:

echo do_shortcode ( '[post type="content" id="294"]' );

echo do_shortcode ( '[post type="title" id="294"]' );

And here is the actual shortcode registration:

// [post type="content" id="2"]
// [post type="title" id="2"]

function post__shortcode( $atts ) {
    $a = shortcode_atts(
        array (
            'id'   => false,
            'type' => "content",
        ), $atts );

    $id   = $a [ 'id' ];
    $type = $a [ 'type' ];

    // bad id
    if ( ! is_numeric( $id ) ) {
        return '';
    }

    // find the post
    $post = get_post( $id );

    // bad post
    if ( ! $post ) {
        return '';
    }

    // allow for other post attributes
    switch ( $type ) {

        case "content":
            return $id === get_the_ID() || $id === get_queried_object_id()
                ? '' // no recursive loops!
                : apply_filters( 'the_content', $post->post_content );

        case "title":
            return $post->post_title;
    }

    // nothing to see here
    return '';
}

add_shortcode( 'post', 'post__shortcode' );

Obviously you could modify the switch to include any type of content you need for that post.

Not the answer you're looking for? Browse other questions tagged or ask your own question.