Support » Fixing WordPress » how to clean up 7 loops on a template

  • Resolved smrdo

    (@smrdo)


    Hi, im using the following to create a kind of time table, bascially i have a custom write panel, with a group ‘When’ cotaining a time slot am or pm, a site (site 1 or site 2) and the day on whcih the course is held.
    At the moment I have this loop repeated for each day, was just wondering if thyer is way of cleaning it up a bit so as to not have it 7 time on the template.

    <h3>Tuesday</h3>
    <?php while (have_posts()) : the_post(); ?>
    
    	<?php $myEvent = get_group('When'); // use the Custom Group name
    	  foreach($myEvent as $event){ ?>
    				<?php if($event['whichDays'][1] == 'Tues') { ?>
    										<p><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a> | <?php echo $event['site'][1] ?></p>
    				<?php } ?>
    	<?php } ?>
    <?php endwhile; ?>
    <br />
Viewing 6 replies - 1 through 6 (of 6 total)
  • I can’t see enough of the code to answer. Is there another while loop for each day? Do you reset the query between days?

    Perhaps something like this would work:

    $daynames = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
    $dayabbrevs = array('Mon','Tue','Wed','Thu','Fri','Sat','Sun');
    foreach ($daynames as $day) {
       echo "<h3>$day</h3>";
       // Rest of code for this day
    }

    Hi yes theres a differnet loop for each day, eg heres mon, tues, weds

    <h3>
      <?php the_title(); ?>
    </h3>
    <br />
    <?php the_content('<p class="serif">Read the rest of this page &raquo;</p>'); ?>
    <?php endwhile; endif; ?>
    <br />
    <?php query_posts('cat=' . $shellCourse  ."&showposts=25&orderby=meta_value&meta_key=site&order=ASC"); ?>
    <h3>Monday</h3>
    <?php while (have_posts()) : the_post(); ?>
    <?php $myEvent = get_group('When'); // use the Custom Group name
    	  foreach($myEvent as $event){ ?>
    <?php if($event['whichDays'][1] == 'Mon') { ?>
    <p><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>">
      <?php the_title(); ?>
      </a> | <?php echo $event['site'][1] ?></p>
    <?php } ?>
    <?php } ?>
    <?php endwhile; ?>
    <br />
    <h3>Tuesday</h3>
    <?php while (have_posts()) : the_post(); ?>
    <?php $myEvent = get_group('When'); // use the Custom Group name
    	  foreach($myEvent as $event){ ?>
    <?php if($event['whichDays'][1] == 'Tues') { ?>
    <p><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>">
      <?php the_title(); ?>
      </a> | <?php echo $event['site'][1] ?></p>
    <?php } ?>
    <?php } ?>
    <?php endwhile; ?>
    <br />
    <h3>Wednesday</h3>
    <?php while (have_posts()) : the_post(); ?>
    <?php $myEvent = get_group('When'); // use the Custom Group name
    	  foreach($myEvent as $event){ ?>
    <?php if($event['whichDays'][1] == 'Weds') { ?>
    <p><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>">
      <?php the_title(); ?>
      </a> | <?php echo $event['site'][1] ?></p>
    <?php } ?>
    <?php } ?>
    <?php endwhile; ?>
    <br />

    this works, just wanted to maybe streamline it a bit, the h3 heading is hard coded

    I don’t have a way to test this, but it should be close:

    <?php $daynames = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
    $dayabbrevs = array('Mon','Tue','Wed','Thu','Fri','Sat','Sun');
    $daycount = 0;
    foreach ($daynames as $day) { ?>
       <?php query_posts('cat=' . $shellCourse  ."&showposts=25&orderby=meta_value&meta_key=site&order=ASC");?>
       <?php echo "<h3>$day</h3>"; ?>
       <?php while (have_posts()) :
           the_post(); ?>
          <?php
          $myEvent = get_group('When'); // use the Custom Group name
          if ($myEvent) {
             foreach($myEvent as $event){ ?>
                <?php if($event['whichDays'][1] == $dayabbrevs[$daycount]) { ?>
                   <p><a href="<?php the_permalink() ?>" title="Permanent Link to <?php the_title_attribute(); ?>">
                   <?php the_title(); ?>
                   </a> | <?php echo $event['site'][1] ?></p>
                <?php } ?>
             <?php } ?>
          <?php } ?>
       <?php endwhile; ?>
       <br />
       <?php ++$daycount;
    } ?>

    fantastic, many thanks.
    I’ll give a go when i get a moment and let you know what happened.
    Either way you’ve pointed me in the right direction anyway…

    Thankyou so much, this worked perfectly

    Great! Now, please use the dropdown at top right to mark this topic ‘Resolved’.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘how to clean up 7 loops on a template’ is closed to new replies.