1

嗨,我已通过模板文件中的自定义代码检索到注释,如下所示

<?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; ?>

现在我不知道如何像 << 1, 2, 3 >>... 那样进行编号分页...请帮帮我

4

3 回答 3

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



?>
于 2013-02-08T07:08:55.007 回答
1

我试图完成与原始海报类似的事情:收集 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);
        ?>
于 2020-10-23T03:03:06.330 回答
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; ?>
于 2021-02-23T09:40:56.830 回答