如何按作者列出帖子?

时间:2011-12-11 作者:Sinthia V

如何将帖子列为作者的子项?它应该看起来像:

<ul>
    <li>Author</li>
        <ul>
            <li>Post1</li>
            <li>Post2</li>
        </ul>
    </li>
</ul>
我有一个包含作者数据的数组,包括$author->ID.我的函数如下所示:

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);
在哪里可以获得帖子列表?

3 个回复
最合适的回答,由SO网友:Rutwick Gangurde 整理而成

如果您想完全控制代码,而不想使用查询,那么这是一个更简单的解决方案。

$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>";
}

?>
您也可以在过滤器函数中使用此选项。

SO网友:Philip

您需要菜单中按作者列出的帖子列表吗?还是在自定义页面中?请在你的问题中说明这一点。

对于菜单,您可以构建自定义菜单,并使用css对其进行样式设置,以满足您的需要。

对于页面,可以构建自定义查询:

<?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; ?>
您可以将其放在自定义页面中
此外,如果需要从查询中排除管理员或任何其他用户,请使用$site_admin 变种。

SO网友:krembo99
   // 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  ?>
结束