输出类别作者已经作为一个类写入(WordPress)

时间:2013-12-18 12:05:26

标签: php wordpress categories

好的,我有一个非常具体的问题,希望有人能说清楚。

我有一个页面列出了使用以下代码输出的作者

<?php

$display_admins = false;
$order_by = 'post_count'; // 'nicename', 'email', 'url', 'registered', 'display_name', or 'post_count'
$role = ''; // 'subscriber', 'contributor', 'editor', 'author' - leave blank for 'all'
$hide_empty = true; // hides authors with zero posts

if(!empty($display_admins)) {
    $blogusers = get_users('orderby='.$order_by.'&role='.$role);
} else {
    $admins = get_users('role=administrator');
    $exclude = array();
    foreach($admins as $ad) {
        $exclude[] = $ad->ID;
    }
    $exclude = implode(',', $exclude);
    $blogusers = get_users('exclude='.$exclude.'&orderby='.$order_by.'&role='.$role.'&order='.'DESC');
}
$authors = array();
foreach ($blogusers as $bloguser) {
    $user = get_userdata($bloguser->ID);
    if(!empty($hide_empty)) {
        $numposts = count_user_posts($user->ID);
        if($numposts < 1) continue;
    }
    $authors[] = (array) $user;
}

echo '<ul class="contributors">';
foreach($authors as $author) {
    $display_name = $author['data']->display_name;
    $avatar = get_wp_user_avatar($author['ID'], 'medium');
    $author_profile_url = get_author_posts_url($author['ID']);
    $filter = get_userdata($author['ID'])->yim;

    echo '<li><div class="home ', $filter,'  "><div class="feature-image"><a href="', $author_profile_url, '">', $avatar , '</a></div>
    <div class="post-title"><a href="', $author_profile_url, '"><h3>', $display_name, '</h3></a></div>
    </div>
    </li>';
}
echo '</ul>';
?>

(我从另一个支持主题得到了这个并调整了它,虽然我不记得在哪里)

目前,$ filter变量只是我在“Yahoo IM”配置文件框中输入的字符串(用于测试过滤器的脏修复)。我希望这实际上是一个类别列表(作为我将输出到作为循环的class =“”部分的slug),作者已发布。

我基本上希望能够按照他们发布的类别过滤作者,并且我正在使用的过滤器(Isotope)使用该类进行操作,因此将类别输出到标记的类是我的意思在我之后。

感激不尽的任何建议!

2 个答案:

答案 0 :(得分:1)

// Returns the posts made by the author
$authorPosts = get_posts("author={$author['ID']}"); 

$categoryList = array(); // reset previous values
foreach ($authorPosts as $post) {
  $postCategories = get_the_category($post->ID);

  // Add to the categories the author has posted in
  foreach ($postCategories as $category)
    $categoryList[] = $category->slug;
} 

// Removes duplicate categories
$categoryList = array_unique($categoryList);

然后,您可以使用$filter = implode(' ', $categoryList);并将其添加到您的HTML中。

答案 1 :(得分:0)

RE没有从其他答案中存储数组,你可以在那里回应那些slu that然后像这样:

$authorPosts = get_posts("author={$author['ID']}"); 

foreach ($authorPosts as $post) {
    $postCategories = get_the_category($post->ID);

    // Add to the categories the author has posted in
    foreach ($postCategories as $category)
        echo($category->slug);
    } 

否则,如果你想把你的PHP放在最顶层,然后在页面的下方回显,那么你可以在那里回显它们:

$i = 0;

foreach($categoryList as $category) {

    echo($categoryList[$i]);
    $i++;

}