Loop through Previous/Next Posts

Question

I’m wondering if there’s a way to, starting at the current post, loop backwards or forwards through all previous or next posts in a given taxonomy, and do something on each iteration. I’d then like the term to break when it reaches a post with a specific term in that taxonomy.

The code below is non-functional dummy code but I think it demonstrates what I’m trying to do. What is the semantically/logically correct way to achieve this?

    <?php if( get_previous_post($in_same_term = true, $excluded_terms = '', $taxonomy = 'my-taxonomy') ) 
    
        while( have_previous_posts($in_same_term = true, $excluded_terms = '', $taxonomy = 'my-taxonomy') ) : 

            if(term == breakterm)

                break;

            else()

                /* Do Something */

        endwhile;
    
     endif; ?>
0
, , , ZackAkai 1 year 2020-07-25T17:10:20-05:00 0 Answers 45 views 0

Leave an answer

Browse
Browse