skip to Main Content

i have an issues in my code below.
In days and time i want to add a loop, it s because i want to be able to get multiple different operating hours row. I ll add a repeater in my custom field.
And for now it s allow me to get only one row.

<ul>
    
            <?php foreach($locations as $location) {
              
              $title = get_the_title($location);
    
              $address = '';
    
              $address .= get_field('address', $location) . '<br>';
              $address .= get_field('city', $location) . ', ';
              $address .= get_field('state', $location) . ', ';
              $address .= get_field('zip_code', $location); 
              $hours = get_field('days_open', $location);
              $phone = get_field('contact_number', $location);
              $email = get_field('contact_email', $location);
              ?>
              
              
    
              <li>
                <h3><?php echo $title; ?></h3>
                <address><?php echo $address; ?></address>
                
                <?php if($phone) { ?>
                    <a href="tel:<?php echo $phone ?>"><?php echo $phone; ?></a>
                <?php }; ?>

                <?php if($email) { ?>
                    <a href="mailto:<?php echo $email ?>"><?php echo $email; ?></a>
                <?php }; ?>

              <?php
                if($hours && $hours[0]['days']
                ) {
                  echo $hours[0]['days'] . ': ';
                }
              ?>
              <?php
                if($hours && $hours[0]['times']
                ) {
                  echo $hours[0]['times'];
                }
              ?>
              </li>
    
              <?php
    
            }
          
            ?>
    
          </ul>

So i think my solution it s to add a foreach loop, can you help for that?
Thank you in advance cheers!

2

Answers


  1. Chosen as BEST ANSWER

    So I found the solutions it was :

    <?php
                  if(is_array($hours)){
                    foreach($hours as $day)
                        {
                          echo $day['days'] . ': ';
                          echo $day['times'];
                          echo '<br />';
                        }
                 } ?>
    

  2. May be this is what you are looking for?

     if(is_array($hours[0]['days'])){
         foreach($hours[0]['days'] as $day)
             {
                 echo $day . ': ';
             }
      }
      if(is_array($hours[0]['times'])){
         foreach($hours[0]['times'] as $time)
             {
                 echo $time . ': ';
             }
      }             
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search