Hello
I'm working on a site, and need to add co-author support to my custom posts, which are then displayed under each user that owns this post. There can be several.
Right now this function only supports 1 author, but I need this changed to multiple authors. Is this possible? I'm using the co-authors plus plugin.
function list_all_authors() {
if ( is_page( 33 /* Rejseledere */ ) ) :
global $wpdb;
$authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users ORDER BY display_name");
?>
<div id="rejseledere" class="clearfix">
<?php foreach ( $authors as $author ) : ?>
<?php $aid = $author->ID; if ( $aid == 1 || $aid == 8 || $aid == 12 ) continue; ?>
<div class="author_info author-<?php the_author_meta( 'user_nicename', $aid ); ?>">
<span class="author_photo"><?php echo get_avatar( $aid, 96 ); ?></span>
<p><h2 style="color: #757a34;"><?php the_author_meta( 'display_name', $aid ); ?></h2></p>
<p><?php the_author_meta('description',$aid); ?></p>
<h4 style="padding-top: 10px;"><strong>Rejser med <?php the_author_meta( 'display_name', $aid ); ?></strong></h4><?php /* </a> */ ?>
<ul class="postcatlist">
<?php
$result = '';
$authposts = get_posts('author=' . $aid . '&numberposts40' . $aid . '&post_type=destination');
foreach ( $authposts as $single ) :
$result .= '<li><a href="' . get_permalink( $single->ID ) . '">' . $single->post_title . '</a></li>';
endforeach;
echo $result;
?>
</ul>
</div>
<?php endforeach; ?>
</div>
<?php
endif;
Francisco Javier Carazo Gil answers:
Hi René,
I don't know this plugin but I have been reading and it's not possible having more than one author in one post.
I recommend you to use meta fields and save user_id inside it.
René Sejling comments:
Why is it not possible when there is a plugin that does it?
Check this out, might help.. http://wordpress.org/extend/plugins/co-authors-plus/other_notes/
Francisco Javier Carazo Gil comments:
Hi René,
Sorry. I have downloaded the plugin and I have seen the code.
You can use this function:
function get_coauthors( $post_id = 0, $args = array() )
Francisco Javier Carazo Gil comments:
In this file: template-tags.php you have useful functions.
René Sejling comments:
Cool
How would I add it in the code above?
I'm not the greatest programmor :)
Thanks
Francisco Javier Carazo Gil comments:
get_coauthors returns an array with objects containing authors data. Each object you can get the data:
<?php
get_coauthors();
foreach($authors as $user_info)
{
echo 'Username: ' . $user_info->user_login . "\n";
echo 'User level: ' . $user_info->user_level . "\n";
echo 'User ID: ' . $user_info->ID . "\n";
}
?>
René Sejling comments:
Can't get it working.
This part is the tricky one, I need this to list each post that each author is added to:
<?php
$result = '';
$authposts = get_posts('author=' . $aid . '&numberposts40' . $aid . '&post_type=destination');
foreach ( $authposts as $single ) :
$result .= '<li><a href="' . get_permalink( $single->ID ) . '">' . $single->post_title . '</a></li>';
endforeach;
echo $result;
?>
But no idea how to add the coauthors info in here. Hope you can help.
Francisco Javier Carazo Gil comments:
René,
Send me FTP credentials via PM and I will do it.
Julio Potier answers:
Code not tested, i printed the coauthors after the permalink in your <li> because i don't know where you wanted to print it.
<?php
$result = '';
$authposts = get_posts('author=' . $aid . '&numberposts=40' . $aid . '&post_type=destination');
foreach ( $authposts as $single ) {
$coauthors = get_coauthors( $single->ID );
foreach ( $coauthors as $coauthor ) {
$all_coauthors[] = $coauthor->user_nicename;
}
$result .= '<li><a href="' . get_permalink( $single->ID ) . '">' . $single->post_title . '</a> - Co-authors: ' . implode( ',', $all_coauthors ) . '</li>';
}
echo $result;
?>
Julio Potier comments:
<em>( take care you wrote "&numberposts40" instead of "&numberposts=40" )</em>