paginate_comments_links()
云策文档标注
概述
paginate_comments_links() 函数用于显示或检索当前文章评论的分页链接。它基于 paginate_links() 实现,适用于单篇文章的评论分页场景。
关键要点
- 函数主要用于生成评论分页链接,支持显示或返回链接字符串或数组。
- 参数 $args 可选,可传递数组或字符串,用于自定义分页行为,如 base、format、total、current 等。
- 默认参数已针对评论分页优化,例如 base 设置为 cpage 查询参数,add_fragment 为 #comments。
- 返回值取决于 'echo' 和 'type' 参数:若 echo 为 true 且 type 非数组,则直接输出;否则返回链接标记或数组。
- 函数仅在单篇文章页面有效,否则返回 void。
代码示例
paginate_comments_links( array(
'prev_text' => 'back',
'next_text' => 'forward'
) );注意事项
- 使用前需在 SETTINGS > DISCUSSION 中启用评论分页选项。
- 参数通过 wp_parse_args() 与默认值合并,修改时需谨慎以避免意外行为。
- 若需使用 HTML 实体(如 «、»),必须使用数组语法传递参数。
原文内容
Displays or retrieves pagination links for the comments on the current post.
Description
See also
Parameters
$argsstring|arrayoptional-
Optional args. See paginate_links() .
More Arguments from paginate_links( … $args )
Array or string of arguments for generating paginated links for archives.
basestringBase of the paginated url. Default empty.formatstringFormat for the pagination structure. Default empty.totalintThe total amount of pages. Default is the value WP_Query‘smax_num_pagesor 1.currentintThe current page number. Default is'paged'query var or 1.aria_currentstringThe value for the aria-current attribute. Possible values are'page','step','location','date','time','true','false'. Default is'page'.show_allboolWhether to show all pages. Default false.end_sizeintHow many numbers on either the start and the end list edges.
Default 1.mid_sizeintHow many numbers to either side of the current pages. Default 2.prev_nextboolWhether to include the previous and next links in the list. Default true.prev_textstringThe previous page text. Default ‘« Previous’.next_textstringThe next page text. Default ‘Next »’.typestringControls format of the returned value. Possible values are'plain','array'and'list'. Default is'plain'.add_argsarrayAn array of query args to add. Default false.add_fragmentstringA string to append to each link. Default empty.before_page_numberstringA string to appear before the page number. Default empty.after_page_numberstringA string to append after the page number. Default empty.
Default:
array()
Source
function paginate_comments_links( $args = array() ) {
global $wp_rewrite;
if ( ! is_singular() ) {
return;
}
$page = get_query_var( 'cpage' );
if ( ! $page ) {
$page = 1;
}
$max_page = get_comment_pages_count();
$defaults = array(
'base' => add_query_arg( 'cpage', '%#%' ),
'format' => '',
'total' => $max_page,
'current' => $page,
'echo' => true,
'type' => 'plain',
'add_fragment' => '#comments',
);
if ( $wp_rewrite->using_permalinks() ) {
$defaults['base'] = user_trailingslashit( trailingslashit( get_permalink() ) . $wp_rewrite->comments_pagination_base . '-%#%', 'commentpaged' );
}
$args = wp_parse_args( $args, $defaults );
$page_links = paginate_links( $args );
if ( $args['echo'] && 'array' !== $args['type'] ) {
echo $page_links;
} else {
return $page_links;
}
}
Changelog
| Version | Description |
|---|---|
| 2.7.0 | Introduced. |
Skip to note 4 content
Codex
Enhanced Comment Display
WordPress makes comments.php files simple to write and edit. It’s simple to easily break comments into pages so that you don’t end up with hundreds of comments all loading on every pageview.
You will need to set up the options in SETTINGS > DISCUSSION for paging to work.
The easiest way to do so is with the following function, which prints out a link to the next and previous comment pages, as well as a numbered list of all the comment pages.
It accepts an array or query-style list of arguments similar to get_posts() or get_terms() .
If you want more control, you can also use the simpler next and previous functions:
next_comments_link( $label = "", $max_page = 0 );and
previous_comments_link( $label = "" );Skip to note 5 content
Codex
Custom Prev-/Next-links
To modify the Prev- and Next-links, you can use the options ‘prev_text’ and ‘next_text’. These are provided by the function paginate_links() .
paginate_comments_links( array( 'prev_text' => 'back', 'next_text' => 'forward' ) );Note: If you want to use HTML entities in your ‘prev_text’ or ‘next_text’, you will have to use the array-based syntax.
paginate_comments_links( array( 'prev_text' => '«', 'next_text' => '»' ) )Skip to note 6 content
Binsaifullah
paginate_comments_links(array( 'screen_reader_text'=> __('Pagination','text_domain'), 'prev_text'=> __('Previous','text_domain'), 'next_text'=> __('Next','text_domain'), ));