Ok so not sure this is the best approach but it seems to work well.
// Run query to get all the published replies for the topic
$widget_query = new WP_Query( array(
'post_type' => bbp_get_reply_post_type(),
'post_parent' => bbp_get_topic_id(),
'ignore_sticky_posts' => true,
'no_found_rows' => true,
'post_status' => 'publish',
) );
// Get the replies per page setting value and the count of the total replies + 1 for the initial topic
$posts_per_page = get_option('_bbp_replies_per_page');
$total_posts = $widget_query->post_count + 1;
// if the total replies is greater than the per page setting pagination is used so need to structure url with page number
if($total_posts > $posts_per_page)
{
// Work out last page number and build url using topic link and last reply id
$total_pages = ceil($total_posts/$posts_per_page);
$url = bbp_get_topic_permalink()."page/".$total_pages."/#post-".$widget_query->posts[0]->ID;
}else{
// Same url as above but without page number as not needed
$url = bbp_get_topic_permalink()."#post-".$widget_query->posts[0]->ID;
}
I then added the following where i wanted to place the link
if ( $widget_query->have_posts() ) {
echo "<a href=\"".$url."\">Go to latest post</a>";
}
Use this PHP Function. Add it to your child themes function.php file or in a functionaility plugin.
function rkk_last_reply_link() {
?>
<a class="rkk-last-reply-link" href="<?php bbp_topic_last_reply_url(); ?>">Go to latest post</a>
<?php
}
add_action( 'bbp_template_before_replies_loop', 'rkk_last_reply_link' );