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条)

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了