如何为自定义代码编写的注释添加分页

时间:2013-02-08 05:57:52

标签: wordpress wordpress-plugin wordpress-theming

您好我已经通过我的模板文件中的自定义代码检索了评论,如下所示

<?php $comments = get_comments();?>
<?php foreach($comments as $comment) : ?>
<?php if ($comment->comment_approved == '0') : ?>
<p class="waiting-message">Your comment is awaiting moderation.</p>
<?php endif; ?>
<?php echo $comment->comment_author; ?>
<?php echo comment_date('n M y'); ?>
<?php echo $comment->comment_content;?>
<?php endforeach; ?>

现在我不知道怎么做编号分页,如&lt;&lt; 1,2,3&gt;&gt; ...请帮帮我

3 个答案:

答案 0 :(得分:6)

define('DEFAULT_COMMENTS_PER_PAGE',5);

$id=get_the_ID();

$page = (get_query_var('page')) ? get_query_var('page') : 1;;

//$page=2;





$limit = DEFAULT_COMMENTS_PER_PAGE;

$offset = ($page * $limit) - $limit;

$param = array(

    'status'=>'approve',

    'offset'=>$offset,

    'post_id'=>$id,

    'number'=>$limit,

);
$total_comments = get_comments(array('orderby' => 'post_date' ,

            'order' => 'DESC',

            'post_id'=>$id,

           'status' => 'approve',

            'parent'=>0));

$pages = ceil(count($total_comments)/DEFAULT_COMMENTS_PER_PAGE);
$comments = get_comments($param );

你的评论将是这样的

这样的分页
<?php

        $args = array(

'base'         => @add_query_arg('page','%#%'),

'format'       => '?page=%#%',

'total'        => $pages,

'current'      => $page,

'show_all'     => False,

'end_size'     => 1,

'mid_size'     => 2,

'prev_next'    => True,

'prev_text'    => __('Previous'),

'next_text'    => __('Next'),

'type'         => 'plain');

// ECHO THE PAGENATION 

echo paginate_links( $args );



?>

答案 1 :(得分:1)

以防万一有人遇到我的情况:

我正在寻找完全符合 OP 想要实现的目标,但在 Woocommerce 的产品单页面上:没有任何效果。主要问题之一是,由于某种原因,如果使用公共查询变量,Woocommerce 将重定向到原始 URL。所以,从@anstrangel0ver 的解决方案开始,这就是我所做的:

由于使用“page”作为查询变量是不可能的,我通过我的functions.php文件添加了一个:

function themeslug_query_vars( $qvars ) {
    $qvars[] = 'review';
    return $qvars;
}
add_filter( 'query_vars', 'themeslug_query_vars' );

然后我稍微修改了他的代码,粘贴在我的产品页面上:

$ID = $product->get_ID();

$page = (get_query_var('review')) ? get_query_var('review') : 1;

$total_comments = get_comments(array(
    'orderby'   => 'post_date',
    'order'     => 'DESC',
    'post_id'   => $ID,
    'status'    => 'approve',
    'parent'    => 0,
));

$per_page = 4;
$pages = ceil(count($total_comments)/$per_page);

$limit = $per_page;
$offset = ($page * $limit) - $limit;

$param = array(
    'status'    => 'approve',
    'offset'    => $offset,
    'post_id'   => $ID,
    'number'    => $limit,
);

// Pagination args
$args = array(
    'base'         => get_permalink($ID). '%_%',
    // 'format'       => 'comment-page-%#%',
    'format'       => '?review=%#%',
    'total'        => $pages,
    'current'      => $page,
    'show_all'     => false,
    'end_size'     => 1,
    'mid_size'     => 2,
    'prev_next'    => true,
    'prev_text'    => __('<'),
    'next_text'    => __('>'),
    'type'         => 'plain',
);

$comments = get_comments($param);

然后使用 foreach 创建我的自定义结构:

<?php foreach ($comments => $comment): ?>
    //your code
<?php endforeach; ?>

答案 2 :(得分:0)

我试图完成与原始海报类似的操作:在Wordpress网站上收集所有评论,并以分页显示它们。

以上答案非常有帮助,但我无法使其完全在我的网站上正常工作。我尝试了一些不同的方法,它对我有用。我将代码发布在这里,以防对他人有所帮助。

        <?php
        //Page variables.
        $page = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $per_page = 10;
        $offset = ( ($page -1) * $per_page);

        //Args - comments (paginated).
        $args1 = array(
            'status' => 'approve',
            'post_status' => 'publish',
            'number' => $per_page,
            'offset' => $offset,
        );

        //Args - comments (not paginated).
        $args2 = array(
            'status' => 'approve',
            'post_status' => 'publish',
        );

        //Get comments (paginated).
        $all_comments1 = get_comments( $args1 );

        //Get comments (not paginated) and count.
        $all_comments2 = get_comments( $args2 );
        $all_comments_num = count( $all_comments2 );

        //Display the paginated comments.
        if ( $all_comments1 ) {
            foreach ( (array) $all_comments1 as $comment ) {
                echo '<section class="news-post"><header><time>' . get_comment_date('n.j.y') . '</time><h5>' . $comment->comment_author . ' on <a href="' . esc_url( get_comment_link( $comment ) ) . '">' . get_the_title( $comment->comment_post_ID ). '</a>:</h5>' . $comment->comment_content . '</section>';
            }
        }

        //Args - "paginate_links".
        $page_args = array(
            'base'         => get_permalink( get_the_ID() ). '%_%',
            'format'       => 'page%#%',
            'total'        => ceil($all_comments_num / $per_page),
            'current'      => $page,
            'show_all'     => True,
            'end_size'     => 2,
            'mid_size'     => 2,
            'prev_next'    => True,
            'prev_text'    => __('« Previous'),
            'next_text'    => __('Next »'),
            'type'         => 'plain',
        );

        //Display the "paginate_links".
        echo paginate_links($page_args);
        ?>