bbPress loading more topics with ajax

Created a plugin which allows you to add one ‘load more’ button on your bbPress topics archive. And obviously, that button click loads more topics and appends it at the end of the topics list.

The plugin can be downloaded from here.

You can use it alongside the pagination. The plugin internally loads the same template files to display new topics, so it integrates with your existing theme without any extra effort.

Once you’ve installed and activated the plugin. You need to add 2-3 lines of code in your template file. The file that we will be editing is loop-topics.php. If you’ve not done already, create a folder inside your active theme, and name it as ‘bbpress’, then open the bbPress plugin folder, copy the file templates/default/bbpress/loop-topics.php and paste it inside the new folder that you created in your theme.
Open the file loop-topics.php and look for

<li class="bbp-body" >

An unaltered file will have the following code in that region

<li>

	<?php while ( bbp_topics() ) : bbp_the_topic(); ?>

		<?php bbp_get_template_part( 'loop', 'single-topic' ); ?>

	<?php endwhile; ?>

</li>

We need to add the code for the ‘load-more’ button there. The code that we need to add is:

<?php if( function_exists( 'bbpresslmt_loadmore_button' ) ): ?>
	<?php bbpresslmt_loadmore_button(); ?>
<?php endif; ?>

So after adding this code, our updated file should contain:

<li>

	<?php while ( bbp_topics() ) : bbp_the_topic(); ?>

		<?php bbp_get_template_part( 'loop', 'single-topic' ); ?>

	<?php endwhile; ?>

	<?php if( function_exists( 'bbpresslmt_loadmore_button' ) ): ?>
		<?php bbpresslmt_loadmore_button(); ?>
	<?php endif; ?>

</li>

That’s all we need to do. If you are viewing topics inside any forum, then topics from inside that forum is loaded. If you are viewing all topics list, all topics are loaded. So no configuration required on that part.

Drop in your comments for any issues with the plugin or any other feedback.


Published by

Chandan Chaudhary

Chandan is a WordPress guy. A programmer at heart. He loves building community sites with WordPress and BuddyPress.

7 thoughts on “bbPress loading more topics with ajax”

    1. To automatically load more topics on scroll, put the following code in your theme’s any javascript file:

      jQuery(document).ready(function($){
      $(window).scroll( function(){
      if( $('.topics-list-load-more').length > 0 ){
      $load_more_container = $('.topics-list-load-more');
      if( $(window).scrollTop() + $(window).height() >= $load_more_container.offset().top + $load_more_container.height() ) {
      $load_more_container.find('a').trigger('click');
      }
      }
      });
      });

      But i see one problem with automatically loading on scroll. Form for adding new topic is below the topics list. Users have to scroll down to get to the form. It’d be difficult if scrolling keeps on loading more and more content. I assume you have already done something about it.

Leave a Reply

Your email address will not be published. Required fields are marked *