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 )
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

|improve this answer|||||
  • Hi @Tom thank you for your fast response, I need another clarification. $contributor_id outputs ID but also post_count, desc and authors. The ID is required and is the first parameter. But will the other optional parameters be ignored if they do not fit the type of parameter as describer in the Codex? Won't that create an error? – FakhriAz Feb 18 at 11:30
  • Yes, if it asks for a string or an array, and you pass instead an object, it will likely generate an error, but that's basic PHP rather than anything specific to WordPress. The real question is why would you pass it something other than what you're supposed to pass it? You should be validating and sanitising things, don't just assume a variable contains what you expect it to. Check user input is in the format you expect, check for error objects and values returned by functions before using them, and if you know a number will never be more than say 200, enforce it by capping it, etc – Tom J Nowell Feb 18 at 12:52
  • I got a complimentary clarification from FreeCodeCamp, I got it now. Thanks, everyone. – FakhriAz Feb 26 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.