Authors Page : A page of authors

The question:

I’m looking for a way to create a parent authors page and then use the author.php for the individual author pages.
I’ve been out of the loop (no pun) for a couple months and am wondering the best way to do it.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

First, Explaining Loops

You’re dealing with two kinds of “loops.” The first is a loop in PHP, the second is a loop in WordPress.

Loop in PHP

kaiser gave you some code that will generate a list of authors on your site. You can iterate through this list and print information on the screen.

// Fetch an array of authors from WordPress and store it in the $authors variable
$authors = new WP_User_Query( array(
    'role' => 'editor',
    'fields' => array(
        'first_name',
        'last_name'
    )
) );

// For each author in $authors, we display their name and a link to their author archive page
foreach ( $authors as $author ) {

    // EXAMPLE: display author first & last name
    echo "{$author->first_name} {$author->last_name}";

    echo get_author_posts_url( $author->ID );
}

If you have two members on your site in the “editor” role, the WP_User_Query() call will return a PHP array with two objects in it, each object representing an author. You loop through that array and write out the information from each object in turn.

The WordPress Loop

WordPress calls its standard post system “The Loop.” It’s a standard set of code that’s used in each page template, index template, archive template, etc to display post content. The standard Loop looks something like this:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    // Code for individual posts goes here

<?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

Inside the Loop (the // Code for individual posts goes here part) you can use special functions like the_title(), the_permalink(), the_content(), etc.

What You Need

You need two pages in your theme. First, you need an author.php, as you already indicated in your question. This will display an archive of each post written by a specific author. For example, http://yoursite.url/author/john-doe will list all posts written by an author with the slug, “john-doe.”

You also need to create a custom page template. I would recommend something called author-parent.php or something along those lines. This template file should use WP_User_Query() to fetch a list of users, display those user names, and display links to individual user archive pages.

Then, you need to create a page in WordPress and assign this custom template to that page. When you visit that page in the browser, WordPress will load your custom template and print out the data you’re looking for.

Now, a Word on Rules

The WordPress Answers Stack Exchange is a question and answer site. You need to ask clear, specific questions with as much detail as possible. If people ask you clarifying questions in comments, those are intended to help you refine, clarify, and further flesh out your question.

Don’t just continue to leave comments. Go back and edit your original question to add extra information. If we can’t figure out what you’re asking, we can’t answer your question.

When any question or answer accumulates more than 20 comments in a short period of time, it is automatically flagged by the system for moderator attention. That many comments means something is wrong.

Comments like:

No helps for newbs? Sad…

And:

Sad… I thought I would actually get some help here…

Do nothing to clarify or contribute to the conversation. They’re off topic, create noise, and make it more difficult for the volunteers on the site to help you.

Please take a moment to review the FAQs for the site. They outline how to ask questions on the site and participate in the community. I have no doubt that you can find answers to your questions here. Just remember, if we’re responding to your questions with “I don’t understand” or if there are long comment strings like above, then you might need to rephrase your question. Or just give an example of what you’re trying to accomplish; even if you don’t know the right technical terms for what you’re doing, explaining it in plain enough language will often be enough.

Method 2

Use internal/built-in classes

WP got …

Example code

$authors = new WP_User_Query( array(
     'role' => 'editor',
     'fields' => array(
          'first_name',
          'last_name'
     )
) );

Then just loop through your results (the authors) in a custom template file and assign this to a normal page.

<?php
/*
Template Name: Editors Page
*/

// Query goes here

foreach ( $authors as $author )
{
    // Inspect the author data
    var_dump( $author );

    // EXAMPLE: display author first & last name
    echo "{$author->first_name} {$author->last_name}";

    // Link to author posts @see        http://codex.wordpress.org/Function_Reference/get_author_posts_url
    get_author_posts_url( $author->ID );
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment