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

誰かが私の状況にいる場合に備えて:

OPが達成しようとしていたことを正確に実行しようとしていましたが、Woocommerceの製品の単一ページでは何も機能しませんでした. 主な問題の 1 つは、パブリック クエリ変数が使用されている場合、何らかの理由で Woocommerce が元の URL にリダイレクトすることでした。だから、@ anstrangel0verの解決策から始めて、私がやったことは次のとおりです。

「ページ」をクエリ変数として使用することは問題外だったので、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 に答える