I managed to display the comment_form with a shortcode (while removing it from the default location) using the code below:
`add_shortcode( 'wpse_comment_form', function( $atts = array(), $content = '' )
{
if( is_singular() && post_type_supports( get_post_type(), 'comments' ) )
{
ob_start();
comment_form();
print( '<style>.no-comments { display: none; }</style>' );
add_filter( 'comments_open', '__return_false' );
return ob_get_clean();
}
return '';
}, 10, 2 );
Code suggested by birgire in this answer: https://wordpress.stackexchange.com/a/177289/26350
For more clarity, here is where I want to get: I need to display both the comments form and the comments list through shortcodes and in different locations. I managed to mimic the same code above to display the comments_template (and later editing the comments.php to remove the comment_form from it, since what I really need to display is the comments list) but the comments list displays 2x, one in the shortcode location and also at the bottom of the post (default location). I tried to use the same code to display the wp_list_comments independently but without success.
If you do not provide an array of comments wo wp_list_comments, then it expects to find that a query has already been done (i.e. in the loop).
If you want to display comments for a post independent of the loop, you can use something like:
$comments = get_comments( array( 'post_id' => $id ) );
wp_list_comments( array( 'per_page' => how many to show ), $comments);
So to put it in a shortcode you would do something like (not tested):
add_shortcode( 'wpse_comment_list', function( $atts = array(), $content = '' )
{
if( isset($atts['id']) && post_type_supports( $atts['id'], 'comments' ) )
{
ob_start();
$comments = get_comments( array( 'post_id' => $atts['id'] ) );
wp_list_comments( array( 'per_page' => how many to show ), $comments);
return ob_get_clean();
}
return '';
}, 10, 2 );
and you would invoke it with [wpse_comment_list id="33"] (or whatever the id is).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With