Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to add latest reply or topic to forum list

  • I spent so long trawling google for an answer to this question, so i am sharing what i came up with. It might not be beautiful code (i patched it together from a number of sources), but it works for me.

    Basically, i have a list of forums on my forum home page. I just wanted to show the latest post within each forum as a teaser below the forum description – not just the title, but the excerpt, too. I can’t believe there’s nothing out there explaining how to do this. It seems like a pretty obvious format for the forum index.

    The following snippet will output the latest reply, with post title and post link below the forum description. If there are no replies, it will output the latest topic instead.

    function jag_add_last_reply() { { 
     $jag_last_reply_id = bbp_get_forum_last_reply_id();
     $jag_last_topic_id = bbp_get_forum_last_topic_id();
    $new_args = array(
        'post_type'=> 'reply',
        'p' => $jag_last_reply_id
    );
    $other_args = array(
        'post_type'=> 'topic',
        'p' => $jag_last_topic_id
    );
     
    $nest_query = new WP_Query( $new_args );
    $another_nest_query = new WP_Query( $other_args );
     
       if ( $nest_query->have_posts() ) : while ( $nest_query->have_posts() ) : $nest_query->the_post();
         $this_post_id=$post->ID;
         $this_post_title= get_the_title();
         $this_post_content= get_the_excerpt();
         $this_post_permalink= get_permalink(); ?>   
                                 
         <a href="<?php echo $this_post_permalink; ?>"><h1><?php echo $this_post_title; ?></h1></a>
         <div class="the_content"><?php echo $this_post_content; ?></div>
                         
       <?php endwhile;
    elseif ( $another_nest_query->have_posts() ) : while ( $another_nest_query->have_posts() ) : $another_nest_query->the_post();
         $this_post_id=$post->ID;
         $this_post_title= get_the_title();
         $this_post_content= get_the_content();
         $this_post_permalink= get_permalink(); ?>   
                                 
         <a href="<?php echo $this_post_permalink; ?>"><h1><?php echo $this_post_title; ?></h1></a>
         <div class="the_content"><?php echo $this_post_content; ?></div>
                         
       <?php endwhile; 
       endif; 
        }} 
    // Hook into action
    add_action('bbp_theme_after_forum_description','jag_add_last_reply');

    Just put this in your theme’s functions.php and it should do the trick. Haven’t figured out how to spit out multiple posts and replies for each forum yet, but this is all i needed. Good luck.

Viewing 5 replies - 1 through 5 (of 5 total)
  • update: this was undesirable because the link to the reply was going to the reply itself, which is just a single post of the ‘reply’ custom post type. I have created this dodgy hack of the above code so the permalink for the replies is still the topic permalink. I’m sure there are a million better ways to do this, but it does the job for me so i thought i’d share the updated version.

    function jag_add_last_reply() { { 
     $jag_last_reply_id = bbp_get_forum_last_reply_id();
     $jag_last_topic_id = bbp_get_forum_last_topic_id();
    
    $new_args = array(
        'post_type'=> 'reply',
        'p' => $jag_last_reply_id
    );
    $post_title_args = array(
        'post_type'=> 'topic',
        'p' => $jag_last_topic_id
    );
    $other_args = array(
        'post_type'=> 'topic',
        'p' => $jag_last_topic_id
    );
    $jag_query = new WP_Query( $post_title_args );
    $nest_query = new WP_Query( $new_args );
    $another_nest_query = new WP_Query( $other_args );
    
       if ( $jag_query->have_posts() ) : while ( $jag_query->have_posts() ) : $jag_query->the_post();
         $this_post_id=$post->ID;
         $this_post_permalink= get_permalink(); ?>   
                                 
         <a href="<?php echo $this_post_permalink; ?>">
    
    <?php endwhile; 
       endif; wp_reset_query();
    
       if ( $nest_query->have_posts() ) : while ( $nest_query->have_posts() ) : $nest_query->the_post();
         $this_post_id=$post->ID;
         $this_post_title= get_the_title();
         $this_post_content= get_the_excerpt(); ?>
                                 
         <h1><?php echo $this_post_title; ?></h1></a>
         <div class="the_content"><?php echo $this_post_content; ?></div>
                         
       <?php endwhile;
    elseif ( $another_nest_query->have_posts() ) : while ( $another_nest_query->have_posts() ) : $another_nest_query->the_post();
         $this_post_id=$post->ID;
         $this_post_title= get_the_title();
         $this_post_content= get_the_excerpt(); ?>   
                                 
         <h1><?php echo $this_post_title; ?></h1></a>
         <div class="the_content"><?php echo $this_post_content; ?></div>
                         
       <?php endwhile; 
       endif; 
        }} 
    // Hook into action
    add_action('bbp_theme_after_forum_description','jag_add_last_reply');

    Julia_B
    Participant

    @julia_b

    I’d like to add this function to my Forum index page. Where do I need to paste this code? Thanks 🙂


    PinkishHue
    Participant

    @pinkishhue

    @julia_b (If you’re still waiting for a reply!) – you need to put this in your functions.php file, more info here: https://codex.bbpress.org/functions-files-and-child-themes-explained/

    This works great, thanks so much for sharing this code!

    I’ve also managed, with very little tweaking, to get this displaying replies under topics within the topics list (this is very rough with some bits commented out but pasting here in case it helps someone)

    
    // https://bbpress.org/forums/topic/how-to-add-latest-reply-or-topic-to-forum-list/
    function jagreplies_add_last_reply() { { 
     $jagreplies_last_reply_id = bbp_get_topic_last_reply_id();
     //$jagreplies_last_topic_id = bbp_get_forum_last_topic_id();
    
    $new_args = array(
        'post_type'=> 'reply',
        'p' => $jagreplies_last_reply_id
    );
    $post_title_args = array(
        'post_type'=> 'topic',
        'p' => $jagreplies_last_topic_id
    );
    $other_args = array(
        'post_type'=> 'topic',
        'p' => $jagreplies_last_topic_id
    );
    $jagreplies_query = new WP_Query( $post_title_args );
    $nest_query = new WP_Query( $new_args );
    $another_nest_query = new WP_Query( $other_args );
    
       if ( $jagreplies_query->have_posts() ) : while ( $jagreplies_query->have_posts() ) : $jagreplies_query->the_post();
         $this_post_id=$post->ID;
         $this_post_permalink= get_permalink(); ?>   
                                 
         <!--<a href="<?php //echo $this_post_permalink; ?>">-->
    
    <?php endwhile; 
       endif; wp_reset_query();
    
       if ( $nest_query->have_posts() ) : while ( $nest_query->have_posts() ) : $nest_query->the_post();
         $this_post_id=$post->ID;
         //$this_post_title= get_the_title();
         //$this_post_content= get_the_excerpt(); 
         $this_post_content= the_content(); ?>
    
         <h1><?php echo $this_post_title; ?></h1></a>
         <div class="the_content"><?php echo $this_post_content; ?></div>
                         
       <?php endwhile;
    elseif ( $another_nest_query->have_posts() ) : while ( $another_nest_query->have_posts() ) : $another_nest_query->the_post();
         $this_post_id=$post->ID;
         //$this_post_title= get_the_title();
         //$this_post_content= get_the_excerpt();
         $this_post_content= the_content(); ?>   
                                      
         <!--<h1><?php //echo $this_post_title; ?></h1></a>-->
         <div class="the_content"><?php echo $this_post_content; ?></div>
                         
       <?php endwhile; 
       endif; 
        }} 
    // Hook into action
    add_action('bbp_theme_after_topic_freshness_author','jagreplies_add_last_reply');

    This is extremely useful in creating a ‘Facebook’ style site where all content can be displayed on one single page (I know, Facebook, yuck! But it’s what ‘the people’ like)

    Now I just need to try to get the bbpress Ajax Replies plugin working in conjunction with this so people can post and reply from that single page. Interesting!

    **edited to add – if using my code above you may just want to change the last line ‘bbp_theme_after_topic_freshness_author’ to a different hook depending on where you want to display it, I am using customised templates but I’m not sure how it would look loading the content there if using the default templates


    PinkishHue
    Participant

    @pinkishhue

    Aah just realised of course that code is showing just the *last* reply, not listing all replies, so I will keep tweaking it and update if I can get it showing *all* replies.


    PinkishHue
    Participant

    @pinkishhue

    Ok got it, needs sprucing up design-wise but this nicely outputs the reply content and date (with permalink) directly under the topic title on the single forum view topics list:

    (With thanks to some code used here – https://bbpress.org/forums/topic/how-to-get-reply-ids-within-topic-loop/)

    // https://bbpress.org/forums/topic/how-to-add-latest-reply-or-topic-to-forum-list/ - SHOW REPLIES ON SINGLE FORUM LIST OF TOPICS
    function jagreplies_add_last_reply() { { 
    $new_args = array(
        'post_type'=> 'reply'
    );
    $topic_id = bbp_get_topic_id();
    $new_args['post_parent'] = $topic_id;
    
    $new_query = new WP_Query( $new_args );
    
    if ( $new_query->have_posts() ) : while ( $new_query->have_posts() ) : $new_query->the_post();
    
         $reply_id=$post->ID;
         $reply_title= get_the_title();
         $reply_date= get_the_date();
         $reply_content= the_content(); 
         $reply_permalink= get_the_permalink(); 
    	?>
    
         <div class="the_content">
    		<?php echo $reply_content; ?>
    		<a href="<?php echo $reply_permalink ?>"><?php echo $reply_date ?></a>
    	</div>
                         
       <?php endwhile; ?>   
       <?php endif; 
    
        }} 
    // Hook into action
    add_action('bbp_theme_after_topic_freshness_author','jagreplies_add_last_reply');
Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.
Skip to toolbar