Skip to:
Content
Pages
Categories
Search
Top
Bottom

Link to latest reply in a topic


  • leo-l
    Participant

    @leo-l

    I’m looking to add a link at the top of a topic that will jump the user to the last reply/post in that topic.

    I’m assuming i can do this by building a link to /#post-[ID OF POST] but im not sure how i can get the ID of the last post to insert into the link

    Any help will be greatly appreciated

Viewing 2 replies - 1 through 2 (of 2 total)

  • leo-l
    Participant

    @leo-l

    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>";
    }
    

    Robkk
    Moderator

    @robkk

    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' );
Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Skip to toolbar