最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论