leo-l (@leo-l)

Forum Replies Created

Viewing 1 replies (of 1 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>";
    }
    
Viewing 1 replies (of 1 total)