如何在自定义评论列表中显示分页?

时间:2020-02-13 作者:Navid M

我使用此代码在用户配置文件中显示最新的注释列表:在函数中。php:

add_shortcode ( \'show_recent_comments\', \'show_recent_comments_handler\' );

function show_recent_comments_handler( $atts, $content = null )
{
    extract( shortcode_atts( array( 
        "count" => 20,
        "pretty_permalink" => 0
        ), $atts ));

    $output = \'\'; // this holds the output

    if ( is_user_logged_in() )
    {
    global $current_user;
    get_currentuserinfo();

    $args = array(
        \'user_id\' => $current_user->ID,
        \'number\' => $count, // how many comments to retrieve
        \'status\' => \'approve\'
        );
    $comments = get_comments( $args );
    if ( $comments )
    {
        $output.= "<ul>\\n";
        foreach ( $comments as $c )
        {
        $output.= \'<li>\';
        if ( $pretty_permalink ) // uses a lot more queries (not recommended)
            $output.= \'<a href="\'.get_comment_link( $c->comment_ID ).\'">\';
        else
            $output.= \'<span id="cm-txt">متن دیدگاه : </span><a href="\'.get_settings(\'siteurl\').\'/?p=\'.$c->comment_post_ID.\'#comment-\'.$c->comment_ID.\'">\';         
        $output.= wp_trim_words($c->comment_content, 20, \'...\');
        $output.= \'</a><p>در پست: \' . get_the_title($c->comment_post_ID);
        $output.= "</li>\\n";
        }
        $output.= \'</ul>\';
        }
    }
    return $output;
}
和在用户评论中。php:

<?php echo do_shortcode( \'[show_recent_comments]\' ); ?>
我想显示分页,例如在用户评论中显示20多条评论列表。php。有什么办法吗?

1 个回复
SO网友:Cadu De Castro Alves

您应该使用appendpaginate_comments_links() 进入$output 像这样:

add_shortcode ( \'show_recent_comments\', \'show_recent_comments_handler\' );

function show_recent_comments_handler( $atts, $content = null )
{
    extract( shortcode_atts( array( 
        "count" => 20,
        "pretty_permalink" => 0
        ), $atts ));

    $output = \'\'; // this holds the output

    if ( is_user_logged_in() )
    {
    global $current_user;
    get_currentuserinfo();

    $args = array(
        \'user_id\' => $current_user->ID,
        \'number\' => $count, // how many comments to retrieve
        \'status\' => \'approve\'
        );
    $comments = get_comments( $args );
    if ( $comments )
    {
        $output.= "<ul>\\n";
        foreach ( $comments as $c )
        {
        $output.= \'<li>\';
        if ( $pretty_permalink ) // uses a lot more queries (not recommended)
            $output.= \'<a href="\'.get_comment_link( $c->comment_ID ).\'">\';
        else
            $output.= \'<span id="cm-txt">متن دیدگاه : </span><a href="\'.get_settings(\'siteurl\').\'/?p=\'.$c->comment_post_ID.\'#comment-\'.$c->comment_ID.\'">\';         
        $output.= wp_trim_words($c->comment_content, 20, \'...\');
        $output.= \'</a><p>در پست: \' . get_the_title($c->comment_post_ID);
        $output.= "</li>\\n";
        }
        $output.= \'</ul>\';
        $output .= paginate_comments_links([\'echo\' => false]); // echo false means that it would return the code instead of echoing it
        }
    }
    return $output;
}