Blog / WordPress/ WordPress Author Information Functions and Code Examples

WordPress Author Information Functions and Code Examples

WordPres作者信息相关函数调用代码

When developing WordPress themes, you often need to retrieve author-related information, such as the post author's display name, ID, or published post count.

This article compiles commonly used WordPress author functions and code snippets to assist developers.

Author Functions

The following table lists common WordPress functions for retrieving author information. These are typically used inside the main post loop (The Loop).

WordPress Code Description
<?php the_author(); ?> Displays the author's public display name.
<?php the_author_description(); ?> Displays the author's biography.
<?php the_author_login(); ?> Displays the author's login username.
<?php the_author_firstname(); ?> Displays the author's first name.
<?php the_author_lastname(); ?> Displays the author's last name.
<?php the_author_nickname(); ?> Displays the author's nickname.
<?php the_author_ID(); ?> Displays the author's ID number.
<?php the_author_email(); ?> Displays the author's email address.
<?php the_author_url(); ?> Displays the author's website URL.
<?php the_author_link(); ?> Displays the author's name linked to their website.
<?php the_author_posts(); ?> Displays the author's published post count.
<?php the_author_posts_link(); ?> Displays the author's name linked to their archive page.

Important Notes

  1. Usage Context: Most functions (e.g., the_author()) must be called inside The Loop to work correctly.
  2. Function Names: Some older names (like the_author_ID()) are shown for reference. Modern code should use lowercase variants (e.g., the_author_id()).
  3. Alternative Function: Use get_the_author_meta() for more flexibility or to work outside The Loop.
  4. Output Escaping: Use 'get_' prefix functions (e.g., get_the_author()) to retrieve values for safe output.
// Example: Safely get and output author email inside the loop
<?php
if ( have_posts() ) :
    while ( have_posts() ) : the_post();
        $author_email = get_the_author_meta('user_email');
        echo esc_html( $author_email );
    endwhile;
endif;
?>

Post a Comment

Your email will not be published. Required fields are marked with *.