“Posts per page” dropdown for BuddyPress single forum topic view

This morning I whipped up a little BuddyPress ditty for the CUNY Academic Commons that allows your members to select how many posts they’d like to see at a time when viewing a single forum topic. It’s not particularly beautiful (for one thing, it requires Javascript to work correctly, though it degrades gracefully by not showing up when no jQuery is available). For that reason, it’s probably not really appropriate for distribution in BuddyPress itself, at least not without some heavy cleanup. Anyway, here it is:

In your theme’s functions.php, place the following function:

[code language=”php”]
/**
* Echoes the markup for the “number of posts per page” dropdown on forum topics
*/
function cac_forums_show_per_page_dropdown() {
global $topic_template;

// Get the current number, so we can preselect the dropdown
$selected = in_array( $topic_template->pag_num, array( 5, 15, 30 ) ) ? $topic_template->pag_num : $topic_template->total_post_count;

// Inject the javascript
?>

jQuery(document).ready( function() {
jQuery(‘div#posts-per-page-wrapper’).show();
jQuery(‘select#posts-per-page’).change(function(){
var url = ‘?topic_page=1&num=’ + jQuery(this).val();
window.location = url;
});
});

3 thoughts on ““Posts per page” dropdown for BuddyPress single forum topic view

  1. Brian Hanifin

    Back in the day… I would insert the HTML of something that needed to degrade when JavaScript was not present using JavaScript. But as they say, there is always more than one way to skin a cat. 🙂

    Reply
  2. Pingback: 10 High Quality WordPress and BuddyPress Tutorials From September 2011

  3. Pingback: 10 High Quality WordPress and BuddyPress Tutorials From September 2011 - WPMU.org

Leave a Reply

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