loop - A code that I could not fully grasp related to foreach & count_user_posts()

Working on my first WordPress theme, I created a page template for authors based on this tutorial which is itself based

Working on my first WordPress theme, I created a page template for authors based on this tutorial which is itself based on the twenty-fourteen theme.

What I could not understand is this :

$post_count = count_user_posts( $contributor_id );

From this code example (check especially the first 10 lines for my first question which will very possibly automatically answer my final questions):

<?php
// Output the authors list.
$contributor_ids = get_users( array(
  'fields'  => 'ID',
  'orderby' => 'post_count',
  'order'   => 'DESC',
  'who'     => 'authors',
));

foreach ( $contributor_ids as $contributor_id ) :
$post_count = count_user_posts( $contributor_id );
  // Move on if user has not published a post (yet).
  if ( ! $post_count ) {
    continue;
  }
?>

<div class="contributor">
  <div class="contributor-info">
    <div class="contributor-avatar"><?php echo get_avatar( $contributor_id, 132 ); ?></div>
    <div class="contributor-summary">
      <h2 class="contributor-name"><?php echo get_the_author_meta( 'display_name', $contributor_id ); ?></h2>
      <p class="contributor-bio">
        <?php echo get_the_author_meta( 'description', $contributor_id ); ?>
      </p>
      <a class="button contributor-posts-link" href="<?php echo esc_url( get_author_posts_url( $contributor_id ) ); ?>">
        <?php printf( _n( '%d Article', '%d Articles', $post_count, 'twentyfourteen' ), $post_count ); ?>
      </a>
    </div><!-- .contributor-summary -->
  </div><!-- .contributor-info -->
</div><!-- .contributor -->

<?php
endforeach;
?>

I have checked the Codex for count_user_posts( ) but I could not understand how the code is aligned with the documentation as the output of the foreach loop $contributor_id is not only the ID but also post_count,desc and authors.

These are supposed to be the second parameters which are optional and are

Single post type or array of post types

as the Codex specifies below:

$post_type (array|string) (Optional) Single post type or array of post types to count the number of posts for. Default value: ‘post’

So how the values of "post_count" ,"desc" and “authors”. fit with the Codex documentation?

Same question for:

get_avatar( $contributor_id, 132 )

and

get_the_author_meta( 'display_name', $contributor_id )

Working on my first WordPress theme, I created a page template for authors based on this tutorial which is itself based on the twenty-fourteen theme.

What I could not understand is this :

$post_count = count_user_posts( $contributor_id );

From this code example (check especially the first 10 lines for my first question which will very possibly automatically answer my final questions):

<?php
// Output the authors list.
$contributor_ids = get_users( array(
  'fields'  => 'ID',
  'orderby' => 'post_count',
  'order'   => 'DESC',
  'who'     => 'authors',
));

foreach ( $contributor_ids as $contributor_id ) :
$post_count = count_user_posts( $contributor_id );
  // Move on if user has not published a post (yet).
  if ( ! $post_count ) {
    continue;
  }
?>

<div class="contributor">
  <div class="contributor-info">
    <div class="contributor-avatar"><?php echo get_avatar( $contributor_id, 132 ); ?></div>
    <div class="contributor-summary">
      <h2 class="contributor-name"><?php echo get_the_author_meta( 'display_name', $contributor_id ); ?></h2>
      <p class="contributor-bio">
        <?php echo get_the_author_meta( 'description', $contributor_id ); ?>
      </p>
      <a class="button contributor-posts-link" href="<?php echo esc_url( get_author_posts_url( $contributor_id ) ); ?>">
        <?php printf( _n( '%d Article', '%d Articles', $post_count, 'twentyfourteen' ), $post_count ); ?>
      </a>
    </div><!-- .contributor-summary -->
  </div><!-- .contributor-info -->
</div><!-- .contributor -->

<?php
endforeach;
?>

I have checked the Codex for count_user_posts( ) but I could not understand how the code is aligned with the documentation as the output of the foreach loop $contributor_id is not only the ID but also post_count,desc and authors.

These are supposed to be the second parameters which are optional and are

Single post type or array of post types

as the Codex specifies below:

$post_type (array|string) (Optional) Single post type or array of post types to count the number of posts for. Default value: ‘post’

So how the values of "post_count" ,"desc" and “authors”. fit with the Codex documentation?

Same question for:

get_avatar( $contributor_id, 132 )

and

get_the_author_meta( 'display_name', $contributor_id )
Share Improve this question asked Feb 18, 2020 at 10:35 FakhriAzFakhriAz 331 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Those parameters are optional, as the codex states:

$post_type

(string) (optional) Post type to count the number of posts for.

    Default: "post"

So it's optional, not required. If it isn't passed in, then the default value is "post".

Optional parameters are a part of PHP, e.g.


function print_number( $number=5 ) {
    echo $number;
}
print_number( 2 ); // prints 2
print_number(); // prints 5

Note that this doesn't mean all parameters are optional. Check the parameter in the docs for the function you're using to see if it's optional, and what its default is

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744739323a4590935.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信