【问题标题】:ACF get sub_field of repeater FROM a parent pageACF 从父页面获取转发器的子字段
【发布时间】:2019-03-09 23:29:51
【问题描述】:

我尝试植入这个

- Page A0 (images + texts)
-- Page A1
-- Page A1
---Page A2

在页面 A1 和 A2 中,我需要从一组字段中检索画廊和文本。 如果我使用普通字段,而不是在组中,我会实现这一点......但我需要留在组字段中。

 <?php
    $post_ID = get_the_ID();
    $parentpost_id = wp_get_post_parent_id( $post_ID );
    $images_images = get_sub_field( "images", $parentpost_id ); //use parents-post field: "broschuren-download"
     ?>

<div class="hero__image-wrapper <?php if (count ($images_images) > 1) { echo "heroslide" ;} ?> ">
       <?php if ( $images_images ) :  ?>
           <?php foreach ( $images_images as $images_image ): ?>
               <img class="hero__image fit-cover" src="<?php echo $images_image['sizes']['large']; ?>" alt="<?php echo $images_image['alt']; ?>" />
           <?php endforeach; ?>
       <?php endif; ?>
    </div>

【问题讨论】:

    标签: wordpress field repeater parent advanced-custom-fields


    【解决方案1】:

    get_sub_field() 函数仅在您在父字段上运行的have_rows() 循环内有效。如果您仍想使用get_sub_field(),则必须执行以下操作:

    <?php
    $post_ID = get_the_ID();
    $parentpost_id = wp_get_post_parent_id( $post_ID );
    
    if( have_rows('broschuren-download', $parentpost_id) ): while( have_rows('broschuren-download', $parentpost_id) ): the_row();
    
    $images_images = get_sub_field('images');
    
    if ( $images_images ):
    ?>
        <div class="hero__image-wrapper <?php if (count ($images_images) > 1) { echo 'heroslide'; } ?>">
            <?php foreach( $images_images as $images_image ): ?>
            <img class="hero__image fit-cover" src="<?php echo $images_image['sizes']['large']; ?>" alt="<?php echo $images_image['alt']; ?>" />
            <?php endforeach; ?>
        </div>
    <?php 
    endif;
    endwhile; endif; 
    ?>
    

    【讨论】:

      猜你喜欢
      • 2020-05-15
      • 2021-12-10
      • 2022-07-07
      • 1970-01-01
      • 1970-01-01
      • 2019-05-24
      • 2014-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多