高级自定义字段 else if

Advanced Custom Fields else if

我正在尝试使用 WordPress 中的高级自定义字段显示 3 个不同地点的营业时间。当我用 else if 语句取出整个中间部分时,它对其他 2 个位置正常工作。一旦我在带有 else if 语句的部分中添加,所有页面的商店营业时间都会变为空白。任何帮助将不胜感激。这是我目前所拥有的:

第一节

<?php if (is_single( 3823 ) ){ ?>


<section class="hours">
  <h3>Outlet Hours:</h3>

    <section class="hours-section">
      <?php if(get_field('store_hours')): ?>
        <?php while(has_sub_field('store_hours')): ?>
          <p class="days"><b><?php the_sub_field('store_hours_day'); ?>:&nbsp;&nbsp;</b></p>
          <?php if (get_sub_field( 'store_hours_closed')  == "open") { ?>
            <p class="hours-hours">
              <?php the_sub_field('store_hours_morning_time'); ?>&nbsp;am
              -
              <?php the_sub_field('store_hours_evening_time'); ?>&nbsp;pm
            </p>
          <?php } ?>
          <?php if (get_sub_field( 'store_hours_closed')  == "closed") { ?>
            <b>Closed</b>
          <?php } ?>
          <br/>
        <?php endwhile; ?>
      <?php endif; ?>
    </section>

</section><!-- .sidebar -->

第二节

<?php else if (is_single( 2284) ){ ?>


<section class="hours">
  <h3>Builder Hours:</h3>

    <section class="hours-section">
      <?php if(get_field('store_hours')): ?>
        <?php while(has_sub_field('store_hours')): ?>
          <p class="days"><b><?php the_sub_field('store_hours_day'); ?>:&nbsp;&nbsp;</b></p>
          <?php if (get_sub_field( 'store_hours_closed')  == "open") { ?>
            <p class="hours-hours">
              <?php the_sub_field('store_hours_morning_time'); ?>&nbsp;am
              -
              <?php the_sub_field('store_hours_evening_time'); ?>&nbsp;pm
            </p>
          <?php } ?>
          <?php if (get_sub_field( 'store_hours_closed')  == "closed") { ?>
            <b>Closed</b>
          <?php } ?>
          <br/>
        <?php endwhile; ?>
      <?php endif; ?>
    </section>

</section><!-- .sidebar -->

第三节

  <?php } 
  else { ?>

<?php $other_page = 47; ?> 

<section class="hours">
  <h3>Retail Hours:</h3>

    <section class="hours-section">
      <?php if(get_field('store_hours', $other_page)): ?>
        <?php while(has_sub_field('store_hours', $other_page)): ?>
          <p class="days"><b><?php the_sub_field('store_hours_day', $other_page); ?>:&nbsp;&nbsp;</b></p>
          <?php if (get_sub_field( 'store_hours_closed', $other_page)  == "open") { ?>
            <p class="hours-hours">
              <?php the_sub_field('store_hours_morning_time', $other_page); ?>&nbsp;am
              -
              <?php the_sub_field('store_hours_evening_time', $other_page); ?>&nbsp;pm
            </p>
          <?php } ?>
          <?php if (get_sub_field( 'store_hours_closed', $other_page)  == "closed") { ?>
            Closed
          <?php } ?>
          <br/>
        <?php endwhile; ?>
      <?php endif; ?>
    </section>

</section><!-- .sidebar -->

您的代码中似乎忘记了一两个大括号。我继续更改您的 if/else 语句以使用替代语法,您在代码块的某些地方使用了该语法,但在整个过程中并不一致。

<?php if (is_single( 3823 ) ) : ?>

    <section class="hours">
        <h3>Outlet Hours:</h3>
        <section class="hours-section">
           <?php if(get_field('store_hours')): ?>
              <?php while(has_sub_field('store_hours')): ?>
                 <p class="days"><b><?php the_sub_field('store_hours_day'); ?>:&nbsp;&nbsp;</b></p>
                 <?php if (get_sub_field( 'store_hours_closed')  == "open") : ?>
                    <p class="hours-hours">
                        <?php the_sub_field('store_hours_morning_time'); ?>&nbsp;am
                        -
                        <?php the_sub_field('store_hours_evening_time'); ?>&nbsp;pm
                     </p>
                 <?php endif; ?>
                 <?php if (get_sub_field( 'store_hours_closed')  == "closed") : ?>
                      <b>Closed</b>
                  <?php endif; ?>
                  <br/>
              <?php endwhile; ?>
              <?php endif; ?>
        </section>
    </section><!-- .sidebar -->

<?php elseif (is_single( 2284) ) : ?>

   <section class="hours">
      <h3>Builder Hours:</h3>
          <section class="hours-section">
              <?php if(get_field('store_hours')): ?>
                 <?php while(has_sub_field('store_hours')): ?>
                     <p class="days"><b><?php the_sub_field('store_hours_day'); ?>:&nbsp;&nbsp;</b></p>
                     <?php if (get_sub_field( 'store_hours_closed')  == "open") : ?>
                     <p class="hours-hours">
                     <?php the_sub_field('store_hours_morning_time'); ?>&nbsp;am
                     -
                     <?php the_sub_field('store_hours_evening_time'); ?>&nbsp;pm
                     </p>
                 <?php endif; ?>
                 <?php if (get_sub_field( 'store_hours_closed')  == "closed") : ?>
                     <b>Closed</b>
                 <?php endif; ?>
                 <br/>
                 <?php endwhile; ?>
             <?php endif; ?>
         </section>
     </section><!-- .sidebar -->

<?php else : ?>

    <?php $other_page = 47; ?> 
        <section class="hours">
            <h3>Retail Hours:</h3>
                <section class="hours-section">
                    <?php if(get_field('store_hours', $other_page)): ?>
                        <?php while(has_sub_field('store_hours', $other_page)): ?>
                            <p class="days"><b><?php the_sub_field('store_hours_day', $other_page); ?>:&nbsp;&nbsp;</b></p>
                            <?php if (get_sub_field( 'store_hours_closed', $other_page)  == "open") : ?>
                            <p class="hours-hours">
                            <?php the_sub_field('store_hours_morning_time', $other_page); ?>&nbsp;am
                            -
                            <?php the_sub_field('store_hours_evening_time', $other_page); ?>&nbsp;pm
                            </p>
                        <?php endif; ?>
                        <?php if (get_sub_field( 'store_hours_closed', $other_page)  == "closed") : ?>
                            Closed
                        <?php endif; ?>
                        <br/>
                    <?php endwhile; ?>
                <?php endif; ?>
            </section>
        </section><!-- .sidebar -->

<?php endif; ?>
You should mention type like option, page ,theme

示例:

$other_page ='2';//pageid
<?php if(get_field('store_hours', $other_page)): ?>