Title: Nested loop or saving state?
Last modified: May 25, 2025

---

# Nested loop or saving state?

 *  [cberendes](https://wordpress.org/support/users/cberendes/)
 * (@cberendes)
 * [1 year ago](https://wordpress.org/support/topic/nested-loop-or-saving-state/)
 * I’d like to create this list of PDFs – under the 2025 bulletins heading – by 
   using DisplayPosts to loop through the corresponding attachments (from the Media
   Library).
 * The issue is that the typically two or three links per Sunday are offset by a
   blank line before and after.
 * I could accomplish that either through nesting loops –
 *     ```wp-block-code
       for date = first sunday through last sunday    for service in date.services         <print text and link for service.bulletin>    end serviceend date
       ```
   
 * or by
 *     ```wp-block-code
       previousSundayDate = "xxx"for service = first service of the year through last service of the year   thisSundayDate = service.SundayDate   if previousSundayDate <> thisSundayDate      <print offset>   <print text and link for service.bulletin>   previousSundaydate = thisSundayDateend service   
       ```
   
 * How do I accomplish either with DisplayPosts? Part of the answer may lie in [https://displayposts.com/2019/01/04/enable-display-posts-within-post-listing/](https://displayposts.com/2019/01/04/enable-display-posts-within-post-listing/)
 * but the way I read that I would have to create a bunch of posts, one for each
   Sunday, using DisplayPosts to loop through the Bulletins for that Sunday, and
   then loop through the bunch of posts. Seems cumbersome, since I don’t need the
   individual posts, one for each Sunday.
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fnested-loop-or-saving-state%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

The topic ‘Nested loop or saving state?’ is closed to new replies.

 * ![](https://ps.w.org/display-posts-shortcode/assets/icon-256x256.jpg?rev=2940963)
 * [Display Posts - Easy lists, grids, navigation, and more](https://wordpress.org/plugins/display-posts-shortcode/)
 * [Support Threads](https://wordpress.org/support/plugin/display-posts-shortcode/)
 * [Active Topics](https://wordpress.org/support/plugin/display-posts-shortcode/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/display-posts-shortcode/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/display-posts-shortcode/reviews/)

## Tags

 * [nested loop](https://wordpress.org/support/topic-tag/nested-loop/)

 * 0 replies
 * 1 participant
 * Last reply from: [cberendes](https://wordpress.org/support/users/cberendes/)
 * Last activity: [1 year ago](https://wordpress.org/support/topic/nested-loop-or-saving-state/)
 * Status: not resolved