render_block_core_comments_pagination_numbers()


WordPressのrender_block_core_comments_pagination_numbers()関数は、コメントのページネーション番号を表示するために使用されます。この関数は、コメントセクションのページネーションを制御し、ユーザーがコメントをナビゲートするための番号付きリンクを生成します。

構文

render_block_core_comments_pagination_numbers( array $args );

引数の説明:

  • $args(array) — ページネーションの設定を指定する配列。以下のキーを持つことができます:
    • 'total'(int) — 総ページ数。
    • 'current'(int) — 現在のページ番号。
    • 'base'(string) — リンクのベースURL。
    • 'format'(string) — ページ番号のURL形式。

例1: 基本的なページネーションの表示

この例では、コメントのページネーション番号を表示します。

<?php render_block_core_comments_pagination_numbers( array( 'total' => 5, 'current' => 1, 'base' => get_permalink() . 'comment-page-%#%', 'format' => '%#%' ) ); ?>

例2: カスタムURLベースを使用したページネーション

カスタムURLベースを使用してページネーションを表示します。

<?php render_block_core_comments_pagination_numbers( array( 'total' => 10, 'current' => 2, 'base' => 'https://example.com/comments/%#%', 'format' => '%#%' ) ); ?>

例3: 現在のページを変更

現在のページを3に設定してページネーションを表示します。

<?php render_block_core_comments_pagination_numbers( array( 'total' => 7, 'current' => 3, 'base' => get_permalink() . 'comment-page-%#%', 'format' => '%#%' ) ); ?>

例4: フォーマットを変更

ページネーションのフォーマットを変更して表示します。

<?php render_block_core_comments_pagination_numbers( array( 'total' => 4, 'current' => 1, 'base' => get_permalink() . 'comment-page-%#%', 'format' => 'page-%#%' ) ); ?>

例5: 総ページ数を動的に取得

総ページ数を動的に取得してページネーションを表示します。

<?php render_block_core_comments_pagination_numbers( array( 'total' => get_comment_pages_count(), 'current' => get_query_var( 'cpage' ), 'base' => get_permalink() . 'comment-page-%#%', 'format' => '%#%' ) ); ?>

例6: カスタムクエリ変数を使用

カスタムクエリ変数を使用してページネーションを表示します。

<?php render_block_core_comments_pagination_numbers( array( 'total' => 6, 'current' => isset( $_GET['comment_page'] ) ? intval( $_GET['comment_page'] ) : 1, 'base' => add_query_arg( 'comment_page', '%#%' ), 'format' => '%#%' ) ); ?>

例7: ページネーションのスタイルをカスタマイズ

CSSクラスを追加してページネーションのスタイルをカスタマイズします。

<?php render_block_core_comments_pagination_numbers( array( 'total' => 8, 'current' => 1, 'base' => get_permalink() . 'comment-page-%#%', 'format' => '%#%', 'add_args' => array( 'class' => 'custom-pagination' ) ) ); ?>

注意点

  • この関数は、コメントセクションでのみ使用することを想定しています。
  • 引数'base''format'は、正しいURL構造を保証するために注意して設定してください。
  • 動的なページネーションを実装する場合、総ページ数と現在のページ番号を正確に取得することが重要です。

関連機能: