Wordpress自定义foreach循环与$ authors陷入错误

时间:2012-07-30 13:54:15

标签: wordpress wordpress-theming

默认wp_list_authors将按以下格式返回作者列表:
<a href="#">AuthorName</a> (PostCount)

我想以下列格式显示包含帖子计数的作者列表:
<a href="#">AuthorName (PostCount)</a>

以下提及作者的custom foreach loop错误(Invalid argument supplied)。是否有任何建议我做错了什么?

<?php
    $args = array(
        'orderby'       => 'post_count', 
        'order'         => 'DESC', 
        'optioncount'   => true, 
        'exclude_admin' => false, 
        'show_fullname' => true,
        'hide_empty'    => false,
        'echo'          => false,
        'style'         => none,
        'html'          => false );

    $author = wp_list_authors($args);
      foreach($author as $author->ID) {
        echo '<li><a href="'.get_the_author_link( $author->name ).'">'.get_the_author().' ('.count_user_posts($author->ID).')</a></li> ';
    }

?>

1 个答案:

答案 0 :(得分:3)

您实际上正在重做wp_list_authors函数已经可以执行的操作。您可以删除整个foreach,只需使用以下代码即可实现您的目标:

$args = array(
    'orderby'       => 'post_count',
    'order'         => 'DESC',
    'optioncount'   => true,
    'exclude_admin' => false,
    'show_fullname' => true,
    'hide_empty'    => false
);

$authors = wp_list_authors( $args );

准确地使用此列表显示列表,因为它将显示在那里。