Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This has been bugging me for ages and can't quite get it working. I'm using the following code in my functions file to enable me to display posts created by the same author as the current single post.

function get_related_author_posts() {
global $authordata, $post;

$authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 1, 'post_type' => 'artistblog' ) );


foreach ( $authors_posts as $authors_post ) {
    $output .= '<div class="artist-blog-thumb"><a href="' . get_permalink( $authors_post->ID ) . '">'. get_the_post_thumbnail( $authors_post->ID,'medium' ) .'</a></div><h3><a href="' . get_permalink( $authors_post->ID ) . '">' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</a></h3>';

}


return $output;
}

This works nicely. However I also want to display the automatically trimmed excerpt for each post. I've tried various methods and none of them have quite worked.

Can anyone help with this please?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

your code is not wrong. However, in my opinion, it would be better to handle the task in a slightly different way.

Usage

In your functions.php:

/* This function returns the related posts in an array of objects. You can set how many items you want retrieve.If not set, the number of items will be set to "1". */
function get_related_author_posts( $items = 1 ) {
  global $authordata, $post;
  $authors_posts = get_posts( 
  array( 
      'author' => $authordata->ID, 
      'post__not_in' => array( $post->ID ), 
      'posts_per_page' => $items, 
      'post_type' => 'artistblog' 
    ) 
  );
  return $authors_posts;
}

In your template file:

<?php 
  $related_posts = get_related_author_posts( 3 );
  foreach( $related_posts as $post ) : setup_postdata( $post ); ?>
  <div class="artist-blog-thumb">
    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'medium' ); ?></a>
  </div>
  <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
  <div class="excerpt">
    <p><?php the_excerpt(); ?></p>
  </div>
<?php endforeach; wp_reset_postdata(); ?>

Let me know if you get stuck.

share|improve this answer
    
Thank you iEmanuele! Always seems so obvious when someone points it out to you. Your suggestion worked perfectly! Cheers D –  user36987 Aug 28 '13 at 13:16
    
@user36987, you're welcome! –  iEmanuele Aug 28 '13 at 13:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.