0

How can I customize my comment link without modifying comment-template.php file line 676:

$link = $link . '#comment-' . $comment->comment_ID;

I would like to get the same link just little modified and it should look like:

$link = $link . '?comments#comment-' . $comment->comment_ID;

And I'm using my comments to show like this:

$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

You could try to add your own custom get_comment_link filter just before you call the wp_list_comments() function:

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

// Display comments
wp_list_comments( $args, $comments);

where our callback is defined as:

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

Hopefully you can adjust this to your needs.

  • yeah... it almost working.. just can't understand why it's missing 4 element $cpage... ? if I turn it off no noticing, url is better.. but comment page after replying always first. :/ – Scorpioniz Nov 30 '15 at 5:58
  • and I have some bad args in my dump prntscr.com/98kyof it's a last array of $args variable – Scorpioniz Nov 30 '15 at 6:59
  • but the args is bad only when I am posting a reply to comment, if i dump at my comments load then I see good args :) – Scorpioniz Nov 30 '15 at 7:10
  • how does the link look like when it fails? @Scorpioniz – birgire Nov 30 '15 at 10:42
  • /comment-page-1/?comments#comment-108 i.e. :) everythjing good with rewriting ?comments#comment- just after replying an answer to parent comment it always returning to page 1.. – Scorpioniz Nov 30 '15 at 11:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.