自定义POST类型中的多个ACF中继器

时间:2021-02-04 作者:Neil Kearney

我正在尝试为自定义帖子类型花园提示创建一个帖子模板(single Garden tip.php)

此页面的url为:

http://evergreenhorticulture-co-uk.stackstaging.com/garden-tip/january/

当我把代码呈现为空白时,我想知道我缺少了什么,或者我需要做什么来解决这个问题

模板代码


<?php get_template_part(\'header\'); ?>

<?php get_template_part(\'includes/inner-post-header\'); ?>

<section class="e-blog-v3 garden-set-pd-sm-lg">
    <div class="container">
        <div class="row">
            <div class="col-sm-12">

            <?php while ( have_posts() ) : the_post(); ?>

                <?php if( have_rows(\'garden_tips\') ): ?>
                
                   <?php $image = get_sub_field(\'banner_image\');?>
                   <?php echo wp_get_attachment_image( $image, \'medium_large\' ); ?>

                
                <h6>Flowers</h6>
                 <ul class="garden_tips_list">
                            <?php while( have_rows(\'flowers\') ): the_row(); ?>
                            <li>
                                <i class="fas fa-leaf"></i> <?php the_sub_field(\'flower_tip\'); ?>
                            </li>
                            <?php endwhile; ?>
                    </ul>


                <h6>Fruit and Veg</h6>
                <ul class="garden_tips_list">
                            <?php while( have_rows(\'fruit_veg\') ): the_row(); ?>
                            <li>
                                <i class="fas fa-leaf"></i> <?php the_sub_field(\'fruit_veg_tip\'); ?>
                            </li>
                            <?php endwhile; ?>
                    </ul>

            <h6>Greenhouse</h6>
                <ul class="garden_tips_list">
                            <?php while( have_rows(\'greenhouse\') ): the_row(); ?>
                            <li>
                                <i class="fas fa-leaf"></i> <?php the_sub_field(\'fruit_veg_tip\'); ?>
                            </li>
                            <?php endwhile; ?>
                    </ul>

            <h6>Garden Maintenance</h6>
                <ul class="garden_tips_list">
                            <?php while( have_rows(\'garden_maintenance\') ): the_row(); ?>
                            <li>
                                <i class="fas fa-leaf"></i> <?php the_sub_field(\'fruit_veg_tip\'); ?>
                            </li>
                            <?php endwhile; ?>
                    </ul>
                <?php endif; ?>

            <?php endwhile; // end of the loop. ?>

            </div>
        </div>
</section>


<?php get_template_part(\'footer\'); ?>

Garden Tips ACF Group

如有任何上述帮助,将不胜感激

谢谢

1 个回复
最合适的回答,由SO网友:robwatson_dev 整理而成

您在模板中混合了一些内容

打开中继器字段,例如您调用的garden_tips 在ACF字段组的名称之后,而不是在中继器的名称之后

例如:

因此,您需要删除该项和结束项<?php endif; ?> 这与模板的下一步相关。

然后看每一部分,例如:花,做这样的事情

<?php if( have_rows(\'flowers\') ): ?>
 <h6>Flowers</h6>
 <ul class="garden_tips_list">
  <?php while( have_rows(\'flowers\') ): the_row(); ?>
  <li>
   <i class="fas fa-leaf"></i> <?php the_sub_field(\'flower_tip\'); ?>
  </li>
  <?php endwhile; ?>
 </ul>
<?php endif; ?>
每个部分都需要这样做。

有了横幅,它就不在中继器内,而您正在使用get_sub_field 而不是get_field.

您还应该确保返回图像对象,而不是ID或URL(其在字段的设置中),然后使用类似的方法获取图像URL本身

$banner = get_field( \'banner_image\' ) ? : false;
if( $banner ):
    $bannerSize = \'medium_large\';
    $bannerUrl = $banner[\'sizes\'][$bannerSize];
    $bannerAlt = $banner[\'title\'];?>
<img src="<?php echo $bannerUrl;?>" alt="<?php echo $bannerAlt;?>">
<?php endif;?>
总的来说,应该是这样的

    <?php get_template_part(\'header\'); ?>
        
        <?php get_template_part(\'includes/inner-post-header\'); ?>
        
        <section class="e-blog-v3 garden-set-pd-sm-lg">
          
          <div class="container">
            
            <div class="row">
              
              <div class="col-sm-12">
                
                <?php while ( have_posts() ) : the_post(); ?>
        
                  $banner = get_field( \'banner_image\' ) ? : false;
        if( $banner ):
            $bannerSize = \'medium_large\';
            $bannerUrl = $banner[\'sizes\'][$bannerSize];
            $bannerAlt = $banner[\'title\'];?>
<img src="<?php echo $bannerUrl;?>" alt="<?php echo $bannerAlt;?>">
<?php endif;?>
    
    
                  
                  <?php if( have_rows(\'flowers\') ): ?>
                  <h6>Flowers</h6>
                  <ul class="garden_tips_list">
                  <?php while( have_rows(\'flowers\') ): the_row(); ?>
                    <li>
                      <i class="fas fa-leaf"></i> <?php the_sub_field(\'flower_tip\'); ?>
                    </li>
                  <?php endwhile; ?>
                  </ul>
                  <?php endif; ?>
        
                  
                  <?php if( have_rows(\'fruit_veg\') ): ?>
                  <h6>Fruit and Veg</h6>
                  <ul class="garden_tips_list">
                  <?php while( have_rows(\'fruit_veg\') ): the_row(); ?>
                    <li>
                      <i class="fas fa-leaf"></i> <?php the_sub_field(\'fruit_veg_tip\'); ?>
                    </li>
                  <?php endwhile; ?>
                  </ul>
                  <?php endif; ?>
        
        
                  <?php if( have_rows(\'greenhouse\') ): ?>
                  <h6>Greenhouse</h6>
                  <ul class="garden_tips_list">
                    <?php while( have_rows(\'greenhouse\') ): the_row(); ?>
                    <li>
                      <i class="fas fa-leaf"></i> <?php the_sub_field(\'fruit_veg_tip\'); ?>
                    </li>
                    <?php endwhile; ?>
                  </ul>
                  <?php endif; ?>
        
                            
                  <?php if( have_rows(\'garden_maintenance\') ): ?>
                  <h6>Garden Maintenance</h6>
                  <ul class="garden_tips_list">
                    <?php while( have_rows(\'garden_maintenance\') ): the_row(); ?>
                    <li>
                      <i class="fas fa-leaf"></i> <?php the_sub_field(\'fruit_veg_tip\'); ?>
                    </li>
                    <?php endwhile; ?>
                  </ul>
                  <?php endif; ?>
        
        
                <?php endwhile; // end of the loop. ?>
        
            </div>
          </div>
        </section>
        
        <?php get_template_part(\'footer\'); ?>

相关推荐