I am trying to make a “contributors” page where there is a list of authors and the categories they have posted in.
I am able to use this code to do this for a single author on a single post page:
<?php
$cat_array = array();
$args=array(
'author' => get_the_author_meta('id'),
'showposts'=>-1,
'caller_get_posts'=>1
);
$author_posts = get_posts($args);
if( $author_posts ) {
foreach ($author_posts as $author_post ) {
foreach(get_the_category($author_post->ID) as $category) {
$cat_array[$category->term_id] = $category->term_id;
}
}
}
$cat_ids = implode(',', $cat_array);
$output = strtr( wp_list_categories( 'include='.$cat_ids.'&title_li=&style=none&echo=0' ), array( '<br />' => ' / ' ) );
echo preg_replace( '@s/sn$@', '', $output );
?>
But when I try and plug that bit into my list authors function (so that each author in the list has their respective categories displayed) The list of categories displays at the top of the page (not in the div where I put the code) and in the div where it’s supposed to display, it simply says “array”. I think this is a question of syntax as I am kind of a PHP newb.
Here is the list authors function:
//My List Authors Function
function my_list_authors($args = '') {
global $wpdb;
global $wp_query;
$author = get_query_var('author');
function authorCats() {
$categories = $wpdb->get_results("
SELECT DISTINCT(terms.term_id) as ID, terms.name, terms.slug, tax.description
FROM $wpdb->posts as posts
LEFT JOIN $wpdb->term_relationships as relationships ON posts.ID = relationships.object_ID
LEFT JOIN $wpdb->term_taxonomy as tax ON relationships.term_taxonomy_id = tax.term_taxonomy_id
LEFT JOIN $wpdb->terms as terms ON tax.term_id = terms.term_id
WHERE 1=1 AND (
posts.post_status = 'publish' AND
posts.post_author = '$author' AND
tax.taxonomy = 'category' )
ORDER BY terms.name ASC
");
foreach($categories as $category) :
echo '<li>
<a href="'.get_category_link( $category->ID ).'" title="'.$category->name.'">
'.$category->name.'
</a>
</li>';
endforeach;
}
$defaults = array(
'optioncount' => false, 'exclude_admin' => true,
'show_fullname' => false, 'hide_empty' => true,
'feed' => '', 'feed_image' => '', 'feed_type' => '', 'echo' => true,
'style' => 'list', 'html' => true
);
$r = wp_parse_args( $args, $defaults );
extract($r, EXTR_SKIP);
$return = '';
/** @todo Move select to get_authors(). */
$users = get_users_of_blog();
$author_ids = array();
foreach ( (array) $users as $user )
$author_ids[] = $user->user_id;
if ( count($author_ids) > 0 ) {
$author_ids = implode(',', $author_ids );
$authors = $wpdb->get_results( "SELECT ID, user_nicename from $wpdb->users WHERE ID IN($author_ids) " . ($exclude_admin ? "AND user_login <> 'admin' " : '') . "ORDER BY display_name" );
} else {
$authors = array();
}
$author_count = array();
foreach ( (array) $wpdb->get_results("SELECT DISTINCT post_author, COUNT(ID) AS count FROM $wpdb->posts WHERE post_type = 'post' AND " . get_private_posts_cap_sql( 'post' ) . " GROUP BY post_author") as $row )
$author_count[$row->post_author] = $row->count;
foreach ( (array) $authors as $author ) {
$link = '';
$author = get_userdata( $author->ID );
$posts = (isset($author_count[$author->ID])) ? $author_count[$author->ID] : 0;
$name = $author->display_name;
if ( $show_fullname && ($author->first_name != '' && $author->last_name != '') )
$name = "$author->first_name $author->last_name";
if( !$html ) {
if ( $posts == 0 ) {
if ( ! $hide_empty )
$return .= $name . ', ';
} else
$return .= $name . ', ';
// No need to go further to process HTML.
continue;
}
$authorAvatar = get_avatar($author->ID);
if ( !($posts == 0 && $hide_empty) && 'list' == $style )
$return .= '
<div class="authorBox">
'.$authorAvatar.'
<table>
<tr>
<td class="authorLabel">Founder & Editor:</td>
<td class="authorData"><a href="#">'.$author->nickname.'</a></td>
</tr>
<tr>
<td class="authorLabel">Location:</td>
<td class="authorData">'. $author->location .'</td>
</tr>
<tr>
<td class="authorLabel">Industry:</td>
<td class="authorData">Advertising</td>
</tr>';
if ( $author->twitter != '' ) {
$return .=
'
<tr>
<td class="authorLabel">Website:</td>
<td class="authorData"><a href="#">'.$author->user_url.'</a></td>
</tr>
<tr class="last">
<td class="authorLabel">Twitter:</td>
<td class="authorData"><a href="#">'.$author->twitter.'</a></td>
</tr>';
} else {
$return .= '
<tr class="last">
<td class="authorLabel">Website:</td>
<td class="authorData"><a href="#">'.$author->user_url.'</a></td>
</tr>';
}
$return .= '
</table>
<div class="bioBox">
<ul>
<li class="bioTab"><a>Bio</a> <span></span></li>
<li class="thinkingTab"><a>Thinking About</a> <span></span></li>
<li><a>Articles</a> <span></span></li>
<li><a>Reactions</a> <span></span></li>
</ul>
</div>
<div class="authorBio tab">
<p class="center">'
.$author->description.
'</p>
</div>
<div class="authorThink tab">
<p class="center">';
$return .=
$cat_array = array();
$args=array(
'author' => get_the_author_meta('id'),
'showposts'=>-1,
'caller_get_posts'=>1
);
$author_posts = get_posts($args);
if( $author_posts ) {
foreach ($author_posts as $author_post ) {
foreach(get_the_category($author_post->ID) as $category) {
$cat_array[$category->term_id] = $category->term_id;
}
}
}
$cat_ids = implode(',', $cat_array);
$output = strtr( wp_list_categories( 'include='.$cat_ids.'&title_li=&style=none&echo=0' ), array( '<br />' => ' / ' ) );
echo preg_replace( '@s/sn$@', '', $output );
$return .= '</p>
</div>
</div>';
}
$return = trim($return, ', ');
if ( ! $echo )
return $return;
echo $return;
}
Any insight or ideas much appreciated, thanks!
Seemed interesting, so here is my version. Not sure about
get_user_by()
, should be more robust way to get objects for authors.You try to debug your PHP script here via the community. The best thing you can do with PHP issues is to find the root-cause on your own so you learn PHP. Learning is the process of making errors and then to understand how to prevent them in the future. This is something that you can only do on your own, it’s nothing a community can do instead of you.
For example, to learn more about arrays, please visit the PHP documentation: Arrays.
Next to false usage of PHP, you might make false use of HTML as well. But the HTML problems are normally easier to find, so just start with one and then with the other. Start with PHP in your case :).