如何在Wordpress中用短代码显示注释模板


How to display the comments_template with a shortcode, in Wordpress?

我使用以下代码成功地显示了带有短代码的comment_form(同时将其从默认位置删除):

`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 );

birgire在这个答案中建议的代码:https://wordpress.stackexchange.com/a/177289/26350

为了更清晰,这里是我想要得到的:我需要通过短代码和不同的位置显示评论表单和评论列表。我设法模仿上面的相同代码来显示comments_template(后来编辑comments.php从中删除comment_form,因为我真正需要显示的是评论列表),但评论列表显示2x,一个在短代码位置,也在帖子底部(默认位置)。我尝试使用相同的代码来独立显示wp_list_comments,但没有成功。

如果您没有提供一个注释数组wo wp_list_comments,那么它会发现查询已经完成(即在循环中)。

如果你想为一篇独立于循环的帖子显示评论,你可以使用这样的东西:

    $comments = get_comments( array( 'post_id' => $id ) );
    wp_list_comments( array( 'per_page' => how many to show ), $comments);

所以要把它放在一个短代码中,你可以做一些类似(未测试)的事情:

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 );

并且您将使用[wpse_comment_list id="33"](或者不管id是什么)来调用它。

我设法得到了我需要的结果,但部分是通过删除子主题中无法通过代码删除的部分。我模仿了用于对显示comment_form(问题中的上面)进行短编码以显示comments_template(下面的这里)的代码。它在我需要的地方显示了它,但并没有像之前的代码为comment_form所做的那样,从帖子底部删除它的"幽灵"。所以我把single.php复制到我的孩子主题中,并删除了所有这些:

<?php
`// If comments are open or we have at least one, load up
    the comment template
    if ( comments_open() || '0' != get_comments_number() )
    comments_template( '', true );
?>

它奏效了。虽然我不确定这是最好的方式,但我对此很好奇。我不再需要在帖子底部发表评论了;我的评论表单现在是不同的表单,也有不同的位置,以防这是唯一的问题。

这是我用来对comments_template进行短编码显示的代码。我需要它只显示注释而不显示表单,所以我从我的子主题中的comments.php中删除了comments_form调用。

add_shortcode( 'wpse_comments_template', function( $atts = array(), $content = '' )
{
    if( is_singular() && post_type_supports( get_post_type(), 'comments' ) )
    {
        ob_start();
        comments_template();
        print(  '<style>#comments-title { display: none; }</style>' );
        return ob_get_clean();
    }
    return '';
}, 10, 2 ); 

如果您在主题的这一部分之前运行[wpse_comments_template]快捷代码

<?php
    if ( comments_open() || '0' != get_comments_number() )
        comments_template( '', true );
?>

那么它正试图使用:

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',         '__return_false'             );
        add_filter( 'get_comments_number',   '__return_zero'              );
        return ob_get_clean();
    }
    return '';
}, 10, 2 );

但这可能会干扰稍后出现的与评论相关的内容,例如侧边栏中的内容。

因此,使用会更准确

/**     
 * 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;
}

并且您不必从主题中删除任何代码。

我在"十五"主题上测试了这一点,结果似乎如预期的那样。