0

How can I list posts as sub-items of authors? It should look like:

<ul>
    <li>Author</li>
        <ul>
            <li>Post1</li>
            <li>Post2</li>
        </ul>
    </li>
</ul>

I have an array containing author data including $author->ID. My function looks like this:

function add_custom_menu_items($items, $args) {

    $authors = get_users('role=author');

    return $items.$authors_str;    
}
add_filter( 'wp_nav_menu_items', 'add_custom_menu_items', 5, 2);

Where can I get a list of posts?

4
  • Can you explain the use case? Where are you trying to do this? Dec 11, 2011 at 6:21
  • He simply wants to display lists of authors with a nested list of their posts. And he is adding authors to the navigation menu for a start. Check the second screenshot of this plugin, that's what he wants wordpress.org/extend/plugins/list-posts-by-author/screenshots
    – Ashfame
    Dec 11, 2011 at 6:41
  • I looked at the code for that, but the comments are in Spanish. Otherwise that's exactly what I need. I am using it as a menu item in a Son-of-a-suckerfish dropdown. And I'm not a he.
    – Sinthia V
    Dec 11, 2011 at 6:50
  • hi @SinthiaV, you need this custom loop in a menu or a page?
    – Philip
    Dec 11, 2011 at 7:59

3 Answers 3

2

This is a simpler solution, if you want a total control over the code and don't want to use a query.

$authors = get_users('role=author');

if(isset($authors) && !empty($authors))
{
    echo "<ul>";
    foreach($authors as $author)
    {
        $posts = get_posts(array('author'=>$author->ID));

        //if this author has posts, then include his name in the list otherwise don't
        if(isset($posts) && !empty($posts))
        {
            echo "<li>".$author->user_nicename."</li>";

            echo "<ul>";
            foreach($posts as $post)
            {
                echo "<li>".$post->post_title."</li>";
            }
            echo "</ul>";
        }
    }
    echo "</ul>";
}

?>

You can also use this in your filter function.

2
  • I modified your solution a bit, but you put me back on the right track. The documentation for get_users() and get_posts() could be a little easier to find. Thankyou.
    – Sinthia V
    Dec 11, 2011 at 19:43
  • You're welcome! Glad I could help! Dec 11, 2011 at 20:17
0

you need a list of posts by author in the menu? or in a custom page? Please make this clear in your question.

For the menu you can build a custom menu and style it with css to fit your needs.

For a page you can build a custom query:

<?php
// Get the authors from the database ordered by user nicename
global $wpdb;
$site_admin = ""; // enter Admin ID to exclude
$query = "SELECT ID, user_nicename from $wpdb->users WHERE ID != '$site_admin' ORDER BY user_nicename";
$author_ids = $wpdb->get_results($query);

// Loop through each author
foreach($author_ids as $author) :
// Get user data
$curauth = get_userdata($author->ID);
// Get link to author page
$user_link = get_author_posts_url($curauth->ID);
?>
    <ul>
        <li><?php echo $curauth->nickname; ?></li>
            <ul>
            <?php $author_query = new WP_Query( 'author='.$curauth->ID.'&posts_per_page=-1&' );
            while ( $author_query->have_posts() ) : $author_query->the_post(); ?>

                <li><?php the_title(); ?></li>

            <?php endwhile; wp_reset_postdata(); ?>
            </ul>
        </li>
    </ul>
<?php endforeach; ?>

you can put this in a custom page.
Also if you need to exclude the Administrator or any other user from the query, use the $site_admin var.

0
   // the following is with Gravatar, Links to author pages ,and css classes-
<?php
    // Arguments to pass to get_users
    $args = array( 'orderby' => 'display_name', 'order' => 'ASC', 'who' => 'authors' );
    // Query for the users
    $authors = get_users( $args ); ?>

          <div id="authors-list"><p><strong>Authors: </strong>
    <?php
    // Loop through all the users, printing their names, with links to their section of the archives
    for ( $i = 0; $i < count( $authors ); ++$i ) {
      $author = $authors[$i];
      echo "<a href='#{$author->user_nicename}'>$author->display_name</a>";
      if ( $i < count( $authors ) - 1 ) {
        echo ' | ';
      }
    } ?>
          </p></div>

    <?php
    // Loop through all the users, and grab posts
    foreach ( $authors as $author ) { ?>
          <a name="<?php echo $author->user_nicename; ?>"></a>
          <div class="author-posts-container" id="author-<?php echo $author->ID; ?>-posts-wrapper">
            <div class="author-avatar" id="author-<?php echo $author->ID; ?>-avatar">
              <?php echo get_avatar( $author->ID, 96 ); ?>
            </div>
            <div class="author-posts-list" id="author-<?php echo $author->ID; ?>-posts">
              <h2><a href="<?php echo get_author_posts_url( $author->ID ); ?>"><?php echo $author->display_name; ?></a></h2>
      <?php
      // now a loop for each user post
      $args = array( 'author' => $author->ID, 'posts_per_page' => -1 );
      $posts = query_posts($args);
      // quasi-loop use get_template_part
      // if exists
      // get_template_part( 'loop', 'all-authors' ); // Pulls in loop-all-authors.php from theme
      if ( have_posts() ) : ?>
            <ul class="author-post-list" id="author-<?php echo $author->ID; ?>-post-list">
        <?php while ( have_posts() ) : the_post(); // Print whatever we want for each post - for now the title and date ?>
              <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a> &mdash; <?php echo get_the_date(); ?></li>
        <?php endwhile; ?>
            </ul><!-- #author-post-list -->
      <?php else: ?>
              <p>This author has no posts published</p>
      <?php endif; ?>
            </div><!-- #author-posts -->
          </div><!-- #author-posts-container -->
    <?php } // End looping  ?>
1
  • This may work, but it came later than Rutwick's, so I didn't get a chance to try it.
    – Sinthia V
    Dec 11, 2011 at 19:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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