[wp-hackers] Shortcode to call comments_template();

Otto otto at ottodestruct.com
Mon Apr 4 03:31:24 UTC 2011


On Sun, Apr 3, 2011 at 10:21 PM, Matt Martz <matt at sivel.net> wrote:
> On Sun, Apr 3, 2011 at 10:19 PM, SWORD Studios <info at swordstudios.net> wrote:
>>
>> Haha, wow, I should be going to bed when that happens (late night).  Thanks
>> that immediately fixed it.
>> Now the comments are coming in above the content in the post.  I have a few
>> paragraphs of content then at the bottom is the shortcode.  However when you
>> view the post it has the comments above everything else.  Any idea why?
>
> For starters shortcodes need to return, and comments_template does an
> include() which is basically echoing.  Likely the only way you will
> get it to work is to output buffer it, put the output into a var and
> then add it to the return.  Of course this will increase memory usage,
> and blah blah blah...Don't feel like typing any more, but you likely
> know of the down falls of output buffering, but it is likely your only
> option here.

Right, trying to call comments_template inside the shortcode just
isn't going to work.

Instead, why not have the comments shortcode set a global variable,
then base your call to comments_template later in the code based on
that global variable. Like this:

function sword_comment_shortcode($sword_comment_atts) {
global $sword_do_comments;
$sword_do_comments = true;
}
add_shortcode('comments-shareit', 'sword_comment_shortcode');

... later ...

if ($sword_do_comments) {
 comments_template();
}

-Otto


More information about the wp-hackers mailing list