Custom comment link

时间:2015-11-27 作者:Scorpioniz

如何在不修改的情况下自定义注释链接comment-template.php 文件行676:

$link = $link . \'#comment-\' . $comment->comment_ID;
我想对相同的链接进行一些修改,它应该是这样的:

$link = $link . \'?comments#comment-\' . $comment->comment_ID;
我用我的评论表达如下:

$page = intval( get_query_var( \'cpage\' ) );
if ( 0 == $page ) {
    $page = 1;
    set_query_var( \'cpage\', $page );
    set_query_var( \'comments\', \'?\' );
}

# We\'ll do 10 comments per page...
# Note that the \'page_comments\' option in /wp-admin/options-discussion.php must be checked
$comments_per_page = 10;
$comments = get_comments( 
    array( 
        \'status\' => \'approve\',
        \'post_id\' => $post -> ID,
    ) 
);

$args = array(
    \'walker\'            => null,
    \'max_depth\'         => \'2\',
    \'style\'             => \'ul\',
    \'type\'              => \'all\',
    \'reply_text\'        => \'Atsakyti\',
    \'page\'              => $page,
    \'per_page\'          => $comments_per_page,
    \'avatar_size\'       => 0,
    \'reverse_top_level\' => null,
    \'reverse_children\'  => \'\',
    \'format\'            => \'xhtml\', // or \'xhtml\' if no \'HTML5\' theme support
    \'short_ping\'        => false,   // @since 3.6
    \'echo\'              => true     // boolean, default is true
);

wp_list_comments( $args, $comments);

1 个回复
最合适的回答,由SO网友:birgire 整理而成

您可以尝试添加自己的自定义get_comment_link 在调用wp_list_comments() 功能:

// Modify comment links
add_filter( \'get_comment_link\', \'wpse_comment_link\', 10, 4 );

// Display comments
wp_list_comments( $args, $comments);
其中,我们的回调定义为:

function wpse_comment_link( $link, $comment, $args, $cpage )
{ 
    // Only run it once
    remove_filter( current_filter(), __FUNCTION__ );

    // Modify the comment link
    return str_replace( \'#comment-\', \'?comments#comment-\', $link );
}
希望您可以根据自己的需要进行调整。