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?
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:
In your template file:
Let me know if you get stuck.