return paginate_comments_links() as array

The documentation specifies:

type (string) (optional) Controls format of the returned value. Possible values are:

  • 'plain' - A string with the links separated by a newline character.
  • 'array' - An array of the paginated link list to offer full control of display.

In order to add twitter bootstrap pagination styling to the wordpress comments pagination, I want to have the return be an array.

When I add $args to the call, i get no return.

My Code:

ol class="commentlist" style="list-style: none;"
    ?php wp_list_comments('type=commentcallback=advanced_comment'); ?
/ol
div class="clear"/div
div class="pagination"
    ?php 
    $args = array(
    'base'         = '%_%',
    'format'       = '?page=%#%',
    'total'        = 1,
    'current'      = 0,
    'show_all'     = False,
    'end_size'     = 1,
    'mid_size'     = 2,
    'prev_next'    = True,
    'prev_text'    = __('« Previous'),
    'next_text'    = __('Next »'),
    'type'         = 'array',
    'add_args'     = False,
    'add_fragment' = ''
    ); 
    paginate_comments_links($args); 
    ? 
/div

Is this because I am using a custom callback for wp_list_comments?

My advanced_comment() callback:

?php
function delete_comment_link($id)
{
    if (current_user_can('edit_post'))
    {
    echo 'a href="'.admin_url("comment.php?action=cdcc=$id").'" class="btn btn-default"del/a ';
    echo 'a href="'.admin_url("comment.php?action=cdcdt=spamc=$id").'" class="btn btn-default"spam/a';
    }
} 
function advanced_comment($comment, $args, $depth)
{
$GLOBALS['comment'] = $comment; ?
li ?php comment_class(); ? id="comment-?php comment_ID() ?"
    div class="panel panel-default"
        div class="panel-heading"?php comment_author(); ? says: ?php echo substr(get_comment_excerpt(), 0, 25) . "..."; ?/div
        div class="panel-body"
            div class="col-md-1"
            ?php echo get_avatar($comment,$size='75',$default='path_to_url' ); ?
            /div
            ?php if ($comment-comment_approved == '0') : ?
            em?php _e('Your comment is awaiting moderation.') ?/em
            br /
            ?php endif; ?

            div class="comment-text col-md-10"
            div class="comment-meta"
                ?php echo get_comment_author_link($comment-ID); ?
            /div
            small
                ?php printf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ??php edit_comment_link(__('(Edit)'),'  ','') ?
            /small             
                ?php comment_text() ?
            /div

            div class="btn-group float-right"
            ?php echo get_comment_reply_link(array_merge( $args, array('depth' = $depth, 'max_depth' = $args['max_depth'], 'before' = "span class='btn btn-default'", 'after' = '/span'))) ?
            ?php delete_comment_link(get_comment_ID()); ?
            /div
            div class="clear"/div
        /div
    /div
/li   
?php 
}

Please note: paginate_comments_links() works fine if I do not pass it $args.

Solution:

ul class="pagination"
    ?php 
        $pages = paginate_comments_links( array( 'echo' = false, 'type' = 'array' ) );
        foreach($pages as $page)
        {
        echo "li" . $page . "/li";
        }
    ?
/ul

Topic twitter-bootstrap paginate-comments-links array Wordpress

Category Web


The first thing to say would be, you can't say you haven't been warned - see codex page paginate_comments_links(), section »Defaults«:

These arguments are mostly to make the call of paginate_links() work, so be careful if you change them.

It's true, paginate_comments_links() is pretty much just a already customized version for comments of paginate_links(), with those defaults to make this work:

  • 'base' => add_query_arg( 'cpage', '%#%' ),
  • 'format' => ,
  • 'total' => $max_page,
  • 'current' => $page,
  • 'echo' => true,
  • 'add_fragment' => '#comments'

What you're doing is overriding the necessary - hence the warning - defaults of paginate_comment_links() with the defaults of paginate_links(), which leads to the comment links not working anymore. If I'm seeing it correctly the only parameter you want to change is type, so this is the only one you have to pass.

Like this:

$args = array( 
    'type' => 'list' 
);
paginate_comments_links( $args );

Or:

paginate_comments_links( array( 'type' => 'list' ) );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.