Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to get previous and next links of topics inside a forum?


  • athep
    Participant

    @athep

    I don’t have a code for this but basically I’d like to see the pagination of topics ahead of me and the others behind me. For example

    
    Topic 1 link
    Topic 2 link
    Topic 3 link <----- I'm here
    Topic 4 link
    Topic 5 link
    

    And once I click on let’s say topic 5 then I’d get:

    
    Topic 3 link
    Topic 4 link
    Topic 5 link <----- I'm here
    Topic 6 link
    Topic 7 link
    

    Apologies in advance, I couldn’t get to code this, I don’t know where to start thus the lack of a snippet

Viewing 11 replies - 1 through 11 (of 11 total)

  • Robin W
    Moderator

    @robin-w

    not sure where you want this to show??


    athep
    Participant

    @athep

    I want this to show on single-topic’s page


    neon67
    Participant

    @neon67

    Apparently it’s a blog-type queue. There is a queue by the fresh date when the post was written.

    And how want you – by the most recent topic or the most recent comment?


    athep
    Participant

    @athep

    I made the list

    
    <?php $args = array( 'post_type' => 'topic', 'post_parent' => bbp_get_forum_id() ); $loop = new WP_Query( $args ); ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
     <a href="<?php echo get_the_permalink(); ?>"><?php the_title(); ?></a>
    <?php endwhile;?>
    

    This gives me all the topics under the current topic’s forum (I’m on single-topic.php)

    Is there a way I could limit the list to 5 links (that’s easy, post_per_page = 5) but on top of that only show the adjacent posts?

    
    Topic 3 link
    Topic 4 link
    Topic 5 link <----- I'm here
    Topic 6 link
    Topic 7 link
    

    In the above example, it’s showing me 2 previous adjacent posts and to next adjacent posts

    What happens is if I set post_per_page to 5 then this happens

    
    Topic 1 link
    Topic 2 link
    Topic 3 link 
    Topic 4 link
    Topic 5 link <----- I'm here
    

    The current link is always last, I realized this is more of a wordpress question but I’d like to try my luck here since I already opened a thread


    Robin W
    Moderator

    @robin-w

    @athep – have you solved this ?


    athep
    Participant

    @athep

    I think I got timed out, not sure if my reply was deleted.

    I haven’t solved it yet, I did find two promising threads though but I couldn’t make sense of it.


    Robin W
    Moderator

    @robin-w

    just had a quick look

    this should get you close, totally untested !!

    <?php $args = array( 'post_type' => 'topic', 'post_parent' => bbp_get_forum_id() ); 
    $loop = new WP_Query( $args );
    //get the current topic id or maybe depending in where you are putting this - bbp_get_reply_topic_id or bbp_get_reply_id()
    $topic_id = bbp_get_topic_id() ;
    while ( $loop->have_posts() ) : $loop->the_post(); 
    	$cur_id = get_the_ID();
    	if ($cur == $topic_id)  {
    		$prev = get_previous_post() ;
    		$prev_id = $prev->ID ;
    		$next = get_next_post();
    		$next_id = $next->ID ;
    	break ;
    	}
    endwhile;
    //so you now have $cur_id, $prev_id and $next_id
    //so create 3 lines
    if (!empty ($prev_id)) {
    	$permalink = get_permalink($prev_id) ;
    	$title = get_the_title ($prev_id) ;
    	echo '<a href="'.$permalink.'">'.$title.'</a>' ;
    }
    if (!empty ($cur_id)) {
    	$permalink = get_permalink($cur_id) ;
    	$title = get_the_title ($cur_id) ;
    	echo '<a href="'.$permalink.'">'.$title.'</a>' ;
    }
    if (!empty ($next_id)) {
    	$permalink = get_permalink($next_id) ;
    	$title = get_the_title ($next_id) ;
    	echo '<a href="'.$permalink.'">'.$title.'</a>' ;
    }
    ?>

    athep
    Participant

    @athep

    Thank you Robin,

    I tried this, I was inserting this in single-topic.php so I was not sure which one would work.

    $topic_id = bbp_get_topic_id() ; works well, the only issue is that once it nears the end, the other forums start leaking in, if prev or next is empty they leak in

    Otherwise it’s working as expected, also I fixed if ($cur == $topic_id) { to if ($cur_id == $topic_id) {


    Robin W
    Moderator

    @robin-w

    hmmm…not sure about the ‘leak in’ bit, how are lines being displayed if $prev etc. is blank?

    you could add a check

    so set

    $forum_id = bbp_get_forum_id() ;

    above the while statement and then check at the appropriate point

    if (wp_get_post_parent_id($prev_id)  != $forum_id) {
    etc.
    }

    athep
    Participant

    @athep

    I set $forum_id = bbp_get_forum_id() ;

    and change these two

    if (!empty ($prev_id) && wp_get_post_parent_id($prev_id) == $forum_id) {

    and

    if (!empty ($next_id) && wp_get_post_parent_id($next_id) == $forum_id) {

    Notice the equal sign, that solved the problem!

    Thank you so much Robin!

    EDIT:

    The last topic links in every forum somehow get left behind, only the last ones.. I’ll tinker around and post my findings


    Robin W
    Moderator

    @robin-w

    great, and yes please post the completed functioning code once you have got it working !!

Viewing 11 replies - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.
Skip to toolbar