Forked from daggerhart/bbpress_recent_replies-shortcode.php
Created
May 17, 2015 08:46
-
-
Save 1wdtv/b8d7d6dbc0674a5c68c2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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; | |
| } | |
| } | |
| // 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 ); | |
| } | |
| $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'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // default values | |
| $post_types = array('reply'); | |
| $meta_key = '_bbp_last_reply_id'; | |
| $posts_per_page = 15; | |
| // get the 15 topics with the most recent replies | |
| $args = array( | |
| 'posts_per_page' => $posts_per_page, | |
| 'post_type' => array('topic'), | |
| 'post_status' => array('publish'), | |
| 'orderby' => 'modified', | |
| 'order' => 'ASC', | |
| 'meta_key' => $meta_key, | |
| '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; | |
| } | |
| } | |
| // get the actual replies themselves | |
| $args = array( | |
| 'posts_per_page' => $posts_per_page, | |
| 'post_type' => $post_types, | |
| 'post__in' => $reply_ids, | |
| 'orderby' => 'date', | |
| 'order' => 'DESC' | |
| ); | |
| // Bail if no replies | |
| $the_query = new WP_Query( $args ); | |
| if (! $the_query->have_posts() ) { | |
| return; | |
| } | |
| // loop through results and output our rows | |
| ?> | |
| <li> | |
| <? | |
| while($query->have_posts()){ | |
| $query->the_post(); | |
| bbp_get_template_part( 'loop', 'single-topic' ); | |
| } | |
| // Reset the $post global | |
| wp_reset_postdata(); | |
| ?> | |
| </li> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment