This tutorial will show you how to create a custom shortcode for your WordPress Commons. With it, you can place them in custom positions, simply by in putting the shortcode. This is helpful if you use a page builder and want to add your WordPress comments anywhere on the page, or want to place your comments in a different section than your theme normally allows.
There are several plugin that allow you to create shortcodes for WordPress comments, but this example will use a simple code snippet. This makes it so you don't need to install yet another plugin, which will probably slow down your WordPress website.
Simply paste the following code (source) snippet into your functions.php, or by using Advanced Scripts:
/**
* Display the comment template with the [wpse_comments_template]
* shortcode on singular pages.
*
* @see http://stackoverflow.com/a/28644134/2078474
*/
add_shortcode( 'wpse_comments_template', function( $atts = array(), $content = '' )
{
if( is_singular() && post_type_supports( get_post_type(), 'comments' ) )
{
ob_start();
comments_template();
add_filter( 'comments_open', 'wpse_comments_open' );
add_filter( 'get_comments_number', 'wpse_comments_number' );
return ob_get_clean();
}
return '';
}, 10, 2 );
function wpse_comments_open( $open )
{
remove_filter( current_filter(), __FUNCTION__ );
return false;
}
function wpse_comments_number( $open )
{
remove_filter( current_filter(), __FUNCTION__ );
return 0;
}
Now, if you want to place your WordPress comments section anywhere on your website, simply paste in the following shortcode, navigate to the front end, and see the comments section in the new position.
[wpse_comments_template]