@henrywright-1 You were able to give me some help before on something similar, Would you be able to help me with the issue in this post?
Hi @3rdaxis,
Which author’s posts would you like to display?
Thanks for the response, I really just need it to detect the author of the profile the link is going to be on since the site will have many authors. Iv tried a few different ways of getting the authors ID out of the loop but nothing Iv tried has worked besides the code i showed in the original post.
Iv used this which works in the Author Bio on each post.
<a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ); ?>" rel="author">
<?php printf( __( 'View all posts by %s <span class="meta-nav"></span>', TEMPLATE_DOMAIN ), get_the_author() ); ?></a>
@3rdaxis
When viewing a member’s profile page, you can use bp_displayed_user_id()
to get the ID of the member. This will then let you display a list of their posts or their latest post. e.g.
$my_query = new WP_Query( 'author=' . bp_displayed_user_id() );
if ( $my_query->have_posts() ) {
while ( $my_query->have_posts() ) {
$my_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
} else {
// no posts found
}
wp_reset_postdata();
But then note, when you navigate away from the member’s profile page, posts will no longer be displayed due to bp_displayed_user_id()
no longer holding a value. To me, this would make sense as if you were viewing the Privacy Policy or About Us pages (for example) you wouldn’t want a member’s posts to show up.
Thanks Henry, from the code you gave me, i was able to put this one together to display a link to a page of the authors posts with this line of code and it works perfect.
<a href="<?php echo esc_url( get_author_posts_url( bp_displayed_user_id() ) ); ?>" rel="author">Read all posts by <?php echo bp_displayed_user_fullname(); ?></a>