ACF从父页面获取转发器的sub_field

时间:2018-10-04 14:15:19

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

我尝试植入这个

- 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>

1 个答案:

答案 0 :(得分:0)

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; 
?>