Skip to content

Instantly share code, notes, and snippets.

@daggerhart
Last active March 6, 2018 11:42
Show Gist options
  • Save daggerhart/09b0dbc7eb766486353b to your computer and use it in GitHub Desktop.
Save daggerhart/09b0dbc7eb766486353b to your computer and use it in GitHub Desktop.
Custom bbpressRecent replies shortcode.
<?php
/*
* Get the most recently replied-to topics, and their most recent reply
*/
function custom_bbpress_recent_replies_by_topic($atts){
$short_array = shortcode_atts(array('show' => 5, 'forum' => false, 'include_empty_topics' => false), $atts);
extract($short_array);
// default values
$post_types = array('reply');
$meta_key = '_bbp_last_reply_id';
// allow for topics with no replies
if ($include_empty_topics) {
$meta_key = '_bbp_last_active_id';
$post_types[] = 'topic';
}
// get the 5 topics with the most recent replie
$args = array(
'posts_per_page' => $show,
'post_type' => array('topic'),
'post_status' => array('publish'),
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => $meta_key,
);
// allow for specific forum limit
if ($forum){
$args['post_parent'] = $forum;
}
$query = new WP_Query($args);
$reply_ids = array();
// get the reply post->IDs for these most-recently-replied-to-topics
while($query->have_posts()){
$query->the_post();
if ($reply_post_id = get_post_meta(get_the_ID(), $meta_key, true)){
$reply_ids[] = $reply_post_id;
}
}
wp_reset_query();
// get the actual replies themselves
$args = array(
'posts_per_page' => $show,
'post_type' => $post_types,
'post__in' => $reply_ids,
'orderby' => 'date',
'order' => 'DESC'
);
$query = new WP_Query($args);
ob_start();
// loop through results and output our rows
while($query->have_posts()){
$query->the_post();
// custom function for a single reply row
custom_bbpress_recent_reply_row_template( $query->current_post + 1 );
}
wp_reset_query();
$output = ob_get_clean();
return $output;
}
add_shortcode('bbpress_recent_replies_by_topic', 'custom_bbpress_recent_replies_by_topic');
/*
* Executed during our custom loop
* - this should be the only thing you need to edit
*/
function custom_bbpress_recent_reply_row_template( $row_number ){
// get the reply title
$title = get_the_title();
// optional title adjustments -- delete or comment out to remove
// remove "Reply To: " from beginning of title
$title = str_replace('Reply To: ', '', $title);
// trim title to specific number of characters (55 characters)
$title = substr( $title, 0, 55);
// trim title to specific number of words (5 words)...
$title = wp_trim_words( $title, 5, '...');
// determine if odd of even row
$row_class = ($row_number % 2) ? 'odd' : 'even';
?>
<div class="bbpress-recent-reply-row <?php print $row_class; ?>">
<div><?php print $title; ?></div>
<div>Excerpt: <?php the_excerpt(); ?></div>
<div>Author: <?php the_author(); ?></div>
<div>Link To Reply: <a href="<?php the_permalink(); ?>">view reply</a></div>
<div>Link To Topic#Reply: <a href="<?php print get_permalink( get_post_meta( get_the_ID(), '_bbp_topic_id', true) ); ?>#post-<?php the_ID(); ?>">view reply</a></div>
<div>Link To Topic/page/#/#Reply: <a href="<?php bbp_reply_url( get_the_ID() ); ?>">view reply paged</a></div>
<div>Date: <?php the_date(); ?></div>
<div>Avatar: <?php print get_avatar( get_the_author_meta( 'ID' ) ); ?></div>
<div>Time Ago: <?php print human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?></div>
<div>bbPress Profile Link: <?php print bbp_user_profile_link( get_the_author_meta( 'ID' ) ); ?></div>
<div>Avatar linked to bbPress Profile:<a href="<?php print esc_url( bbp_get_user_profile_url( get_the_author_meta( 'ID' ) ) ); ?>"><?php print get_avatar( get_the_author_meta( 'ID' ) ); ?></a></div>
</div>
<?php
// Refs
// http://codex.wordpress.org/Template_Tags#Post_tags
// http://codex.wordpress.org/Function_Reference/get_avatar
// http://codex.wordpress.org/Function_Reference/human_time_diff
// (template tags for bbpress)
// https://bbpress.trac.wordpress.org/browser/trunk/src/includes/users/template.php
// https://bbpress.trac.wordpress.org/browser/trunk/src/includes/replies/template.php
}
// allow shortcodes to run in widgets
add_filter( 'widget_text', 'do_shortcode');
// don't auto-wrap shortcode that appears on a line of it's own
add_filter( 'widget_text', 'shortcode_unautop');
@daggerhart
Copy link
Author

@daggerhart
Copy link
Author

Updated to allow for recent topics that don't yet have replies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment