Group by custom date field
-
I managed to adopt my loop so that it checks for a change in the custom year field and if so outputs a subheading – seems to work great.
However, I need to have the subheading and the records beneath it in an accordian format which requires that the subheading has the class of accordian-title and the associated records beneath have the class of accordian-panel.
Because the subheading is currently being output within the accordian-panel as opposed to above it, my accordian code isn’t working.
eg. html needs to be:
<h3 class="accordian-title</h3> <div class="accordian-panel</div>
Currently the code outputs the title inside the .accordian panel div.
Any guidance would be appreciated – I possibly need a foreach loop somewhere?
global $wp_query; $wp_query = new WP_Query( $args ); //Establish initial variable - variable is tested against current record year and when it changes/doesn't match, it outputs a subheading for the year $yearChecker = " "; if ( have_posts() ) : echo '<div class="accordian-panel>'; while ( have_posts() ) : the_post(); //Variables $mediaReleaseTitle = get_the_title(); $link = get_field('pdf_link'); $year = get_field( 'media_release_date'); //Year Subheading - check if yearChecker is not the same as the currentRecord and if not output subheading if ( $yearChecker != $year ) { echo '<h3 class="accordian-title">Media Releases for ' . $year . '</h3>'; } //Set yearChecker to current record $yearChecker = $year; //Loop Output ?> <div> <h3 class="entry-title" itemprop="headline"><a href="<?php echo $link; ?>" target="_blank"><?php echo $mediaReleaseTitle; ?></a></h3> <div> <?php echo $year; ?></div> </div> <?php endwhile; echo '</div>'; endif; wp_reset_query(); }
The page I need help with: [log in to see the link]
- The topic ‘Group by custom date field’ is closed to new replies.