Skip to content

Instantly share code, notes, and snippets.

@mvaneijgen
Last active September 20, 2022 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mvaneijgen/6437dc91d2778ec6fb9f2eb579756bd7 to your computer and use it in GitHub Desktop.
Save mvaneijgen/6437dc91d2778ec6fb9f2eb579756bd7 to your computer and use it in GitHub Desktop.
Fixing bbPress 2.6 threaded (nested) replies & paging (original code [wpup.co](https://wpup.co/bbpress-threaded-nested-replies-with-paging/)) (see [bbpress forum for discusion](https://bbpress.org/forums/topic/what-is-the-status-of-threaded-nested-replies-and-pagination/#post-207457))
<?php
//------------------------------------------------------//
// Add pagination to topics
//------------------------------------------------------//
function wpup_bbp_list_replies($args)
{
// Reset the reply depth
bbpress()->reply_query->reply_depth = 0;
// In reply loop
bbpress()->reply_query->in_the_loop = true;
$r = bbp_parse_args($args, array(
'walker' => null,
'max_depth' => bbp_thread_replies_depth(),
'style' => 'ul',
'callback' => null,
'end_callback' => null
), 'list_replies');
// Get replies to loop through in $_replies
$walker = new BBP_Walker_Reply;
$walker->paged_walk(bbpress()->reply_query->posts, $r['max_depth'], $r['page'], $r['per_page'], $r);
bbpress()->max_num_pages = $walker->max_pages;
bbpress()->reply_query->in_the_loop = false;
}
//Custom Pagination function
function wpup_custom_pagination($numreplies = '', $pagerange = '', $paged = '', $repliesperpage = '')
{
/**
* $pagerange
* How many pages to display after the current page
* Used in combination with 'shaw_all' => false
*/
if (empty($pagerange)) {
$pagerange = 3;
}
/**
* $numreplies
* What is the total number of replies in the current topic
* $numpages
* Calculate total number of pages to display based on number of replies and replies per page
*/
if ($numreplies != '') {
$numpages = ceil($numreplies / $repliesperpage);
}
//assign value of 1 to $paged variable in case it's not passed on
global $paged;
if (empty($paged)) {
$paged = 1;
}
/**
* We construct the pagination arguments to enter into our paginate_links
* function.
*/
$pagination_args = array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'total' => $numpages,
'current' => $paged,
'show_all' => False,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => True,
'prev_text' => __('&lt;'),
'next_text' => __('&gt;'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<nav class='custom-pagination'>";
echo $paginate_links;
echo "</nav>";
}
}
// END Add pagination to topics -------------------------------------//
<?php
/**
* Replies Loop
*
* @package bbPress
* @subpackage Theme
*/
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$bbpressRepliesPerPage = 20;
?>
<div id="pagination-links">
<?php
// Change $bbpressRepliesPerPage to number of replies you specified in wpup_bbp_list_replies
// First find the number of parent posts only and pass that to the custom pagination function
$replyposts = bbpress()->reply_query->posts;
$numparentreplies = 0;
foreach ($replyposts as $value) {
if ($value->reply_to == 0) {
$numparentreplies++;
}
}
wpup_custom_pagination($numparentreplies, "", $paged, $bbpressRepliesPerPage); ?>
</div>
<?php $replyposts = bbpress()->reply_query->posts; ?>
<ul id="topic-<?php bbp_topic_id(); ?>-replies" class="forums bbp-replies">
<li class="bbp-body">
<?php if (bbp_thread_replies()) : ?>
<?php wpup_bbp_list_replies(array('page' => $paged, 'per_page' => $bbpressRepliesPerPage)); ?>
<?php else : ?>
<?php while (bbp_replies()) : bbp_the_reply(); ?>
<?php bbp_get_template_part('loop', 'single-reply'); ?>
<?php endwhile; ?>
<?php endif; ?>
</li>
</ul>
<div id="pagination-links">
<?php
//Change $bbpressRepliesPerPage to number of replies you specified in wpup_bbp_list_replies
//First find the number of parent posts only and pass that to the custom pagination function
$replyposts = bbpress()->reply_query->posts;
$numparentreplies = 0;
foreach ($replyposts as $value) {
if ($value->reply_to == 0) {
$numparentreplies++;
}
}
wpup_custom_pagination($numparentreplies, "", $paged, $bbpressRepliesPerPage); ?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment