^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数get_comment_pages_count()用法示例

get_comment_pages_count( array $comments = null,  int $per_page = null,  bool $threaded = null )

Calculate the total number of comment pages.


描述


参数

$comments

(array)
(Optional)
array of WP_Comment objects. Defaults to $wp_query->comments

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包

Default value: null

$per_page

(int)
(Optional)
comments per page.

Default value: null

$threaded

(bool)
(Optional)
control over flat or threaded comments.

Default value: null


返回值

(int) Number of comment pages.


源代码

File: wp-includes/comment.php

function get_comment_pages_count( $comments = null, $per_page = null, $threaded = null ) {
	global $wp_query;

	if ( null === $comments && null === $per_page && null === $threaded && !empty($wp_query->max_num_comment_pages) )
		return $wp_query->max_num_comment_pages;

	if ( ( ! $comments || ! is_array( $comments ) ) && ! empty( $wp_query->comments )  )
		$comments = $wp_query->comments;

	if ( empty($comments) )
		return 0;

	if ( ! get_option( 'page_comments' ) ) {
		return 1;
	}

	if ( !isset($per_page) )
		$per_page = (int) get_query_var('comments_per_page');
	if ( 0 === $per_page )
		$per_page = (int) get_option('comments_per_page');
	if ( 0 === $per_page )
		return 1;

	if ( !isset($threaded) )
		$threaded = get_option('thread_comments');

	if ( $threaded ) {
		$walker = new Walker_Comment;
		$count = ceil( $walker->get_number_of_root_elements( $comments ) / $per_page );
	} else {
		$count = ceil( count( $comments ) / $per_page );
	}

	return $count;
}

更新日志

Version 描述
2.7.0 Introduced.

相关函数

Uses

  • wp-includes/query.php:
    get_query_var()
  • wp-includes/class-wp-walker.php:
    Walker::get_number_of_root_elements()
  • wp-includes/option.php:
    get_option()

Used By

  • wp-includes/link-template.php:
    get_the_comments_navigation()
  • wp-includes/link-template.php:
    get_next_comments_link()
  • wp-includes/link-template.php:
    paginate_comments_links()
  • wp-includes/comment-template.php:
    wp_list_comments()
  • wp-includes/comment-template.php:
    comments_template()

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note

    Contributed by Codex

    Basic Usage

    This function can be used within the loop like this:

    
    $pages = get_comment_pages_count();
    
  2. Number of comments per page

    This will use the defaults for the number of comments per page and threading. You can use custom values like this:

    
    // Show 25 comments per page.
    $pages = get_comment_pages_count( null, 25 );
    
    // Don't thread comments.
    $pages = get_comment_pages_count( null, null, false ); 
    
    // Show 10 comments per page, use threading.
    $pages = get_comment_pages_count( null, 10, true ); 
    

    Outside the loop

    When inside the loop, you can just pass null as the value for the $comment parameter, as shown above. You can also use the function outside the loop, but you need to pass in the array of comments. For example, you may perform a custom comment query using WP_Comment_Query:

    
    $args = array(
       // query args here
    );
    
    $comments_query = new WP_Comment_Query;
    $comments = $comments_query->query( $args );
    
    $pages = get_comment_pages_count( $comments );
    

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈