Skip Navigation

[Resolved] Display comments at the middle of a content template

This thread is resolved. Here is a description of the problem and solution.

Problem:
I would like to change the position of where the comments are displayed,
and display them somewhere inside a content template.

I thought about one type of solution, I added a shortcode in functions.php:

add_shortcode( 'display_comments', 'display_comments_shortcode' );
function display_comments_shortcode() {
comments_template();
}

then I added the shortcode somewhere inside the content template, but the comments where displayed at the top of the page, above everything in the page, and not where I located the shortcode.
Solution:
The WordPress function comments_template() will output the HTML codes directly, in the shortcode PHP function, you will need to setup it as return, so you can modify your codes as below:

add_shortcode( 'display_comments', 'display_comments_shortcode' );
function display_comments_shortcode() {
 ob_start();
 comments_template();
 $res = ob_get_contents();
 ob_end_clean();
 return $res;
}

Relevant Documentation:
http://php.net/manual/en/function.ob-get-contents.php

This support ticket is created 7 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 4 replies, has 4 voices.

Last updated by sandeepc 7 years, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#412796

Hello, I have a questions about comments.
I would like to change the position of where the comments are displayed,
and display them somewhere inside a content template.

I thought about one type of solution, I added a shortcode in functions.php:

add_shortcode( 'display_comments', 'display_comments_shortcode' );
function display_comments_shortcode() {
comments_template();
}

then I added the shortcode somewhere inside the content template, but the comments where displayed at the top of the page, above everything in the page, and not where I located the shortcode.

I would appreciate help on this or any other solution.
Thanks

#412863

Dear shirlyk,

The WordPress function comments_template() will output the HTML codes directly, in the shortcode PHP function, you will need to setup it as return, so you can modify your codes as below:

add_shortcode( 'display_comments', 'display_comments_shortcode' );
function display_comments_shortcode() {
 ob_start();
 comments_template();
 $res = ob_get_contents();
 ob_end_clean();
 return $res;
}

More help:
hidden link

#414982

it works, thank you very much!

#566347

I have used this method to display comments for a cpt within a bootstrap tab pane in a layouts content template, and have just realised that the form is displaying correctly, but not the comments. Any thoughts?

<ul class="nav nav-tabs">
<li class="active tab1"><a data-toggle="tab" href="#tab1">Show Description</a>
<li class="tab2"><a data-toggle="tab" href="#tab2">Tickets & Prices</a>
<li class="tab3"><a data-toggle="tab" href="#tab3">Feedback</a>
[wpv-conditional if="( $(wpcf-show_media) ne '' )"]<li class="tab4"><a data-toggle="tab" href="#tab4">Media</a>[/wpv-conditional]

<div class="tab-content">
<div id="tab1" class="tab-pane fade in active mainconts">
<h2>[wpv-post-title]</h2>
[wpv-post-body view_template="None"]
</div>
<div id="tab2" class="tab-pane fade">
<h2>[wpv-post-title] - Tickets</h2>
[wpv-autop][types field='ticket_prices'][/types][/wpv-autop][wpv-view name="show-dates-july-17" limit="-1"]
</div>
<div id="tab3" class="tab-pane fade">
<h2>[wpv-post-title] - Comments</h2>
<hr />
[display_comments]
</div>
<div id="tab4" class="tab-pane fade">
<h2>[wpv-post-title] - media</h2>
[types field='show_media'][/types]
</div>
</div>

#1724573

Dear sir,
After using this short code , i am getting two comment box . one in the shortcode position and other one in the bottom default position.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.