
-
AuthorPosts
-
September 28, 2018 at 2:54 pm #1015757
How Can I display related posts by the same author rather than using a tag ?
-
This topic was modified 6 years, 9 months ago by
TrueCourseCommunications.
September 28, 2018 at 11:34 pm #1015953Hey TrueCourseCommunications,
Try adding this to functions.php
function wpb_related_author_posts($content) { if ( is_single() ) { global $authordata, $post; $content .= '<h4>Similar Posts by The Author:</h4> '; $authors_posts = get_posts( array( 'author' => $authordata->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 5 ) ); $content .= '<ul>'; foreach ( $authors_posts as $authors_post ) { $content .= '<li><a href="' . get_permalink( $authors_post->ID ) . '">' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</a></li>'; } $content .= '</ul>'; return $content; } else { return $content; } } add_filter('get_the_content','wpb_related_author_posts');
Best regards,
Jordan ShannonOctober 1, 2018 at 3:20 pm #1016659Working. But is there a way to display it the same way as the related posts are displayed. In a grid format using thumbnails
-
This reply was modified 6 years, 9 months ago by
TrueCourseCommunications.
October 1, 2018 at 9:48 pm #1016763Hi,
This should be possible, but will require styling via CSS to match what you need.
Best regards,
Jordan ShannonOctober 1, 2018 at 9:50 pm #1016764I have the css. I just need to know how to place the images in a grid format.
Thanks
October 2, 2018 at 6:10 am #1016879Hi,
Thanks for the update, where can we see the results you are getting?
Best regards,
RikardOctober 2, 2018 at 2:59 pm #1017087I am testing it on the local host. The related-posts template is working fine as you can see on https://recipes.instantpot.com/recipe/french-onion-soup/
But that is displaying it using tag. I want to use the same template but by using author name rather than tags.October 4, 2018 at 5:26 am #1017673Hi,
You need to modify the includes > related-posts.php file for that. Use the same get_posts query as the filter above on line 75.
$my_query = get_posts( array( 'tag__in' => $tag_ids, 'post_type' => get_post_type($this_id), 'showposts'=>$postcount, 'ignore_sticky_posts'=>1, 'orderby'=>'rand', 'post__not_in' => array($this_id)) );
You will also need to replace $tags (line 58) to get the author info or id instead.
$tags = get_post_field( 'post_author', $this_id );
And change the subsequent logic base on the author info.
Best regards,
Ismael -
This topic was modified 6 years, 9 months ago by
-
AuthorPosts
- You must be logged in to reply to this topic.