douxi8119 2013-12-18 12:05
浏览 25
已采纳

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

OK, I have a very specific question that I hope someone can shed some light on.

I have a page that lists authors outputting using the following code

<?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>';
?>

(I got this from another support topic and tweaked it, although I can't remember where)

At the moment, the $filter variable is just a string I enter in the 'Yahoo IM' profile box (a dirty fix to test the filter). I'd like this to actually be a list of the categories (as slugs that I will output in to the class="" part of the loop) that the author has posted in.

I essentially want to be able to filter the authors by category that they have posted in, and the filter I'm using (Isotope) operates using the class, so outputting the categories in to the class of the markup is what I'm after.

Any suggestions gratefully received!

  • 写回答

2条回答 默认 最新

  • dswm97353 2013-12-18 13:19
    关注
    // 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);
    

    You can then use $filter = implode(' ', $categoryList); and add it to your html.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备