【问题标题】:get first row (image) of ACF repeater field获取 ACF 转发器字段的第一行(图像)
【发布时间】:2014-02-03 20:03:19
【问题描述】:

我在我的网站上使用 ACF 插件。 我想在一个页面上显示来自子页面的转发器字段的第一行(图像 URL)。

这是我网站的页面(所有图片都已加载,但只显示第一个,但我想只加载第一个)

http://www.amsbooking.fr/mattieumoreau/artists/

这是我页面上显示所有图像的代码:

        <?php

        $args = array(
        'post_type'      => 'page',     
        'post_parent'    => $post->ID,
        'order'          => 'ASC',
        'orderby'        => 'menu_order'
        );

        $parent = new WP_Query( $args );

        if ( $parent->have_posts() ) : ?>

        <?php while ( $parent->have_posts() ) : $parent->the_post(); ?>

        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">

        <div class="cartouche_menu_artistes">

        <div id="parent-<?php the_ID(); ?>" class="parent-page">

        <div class="cartouche_crop">

        <?php while(has_sub_field('photos_presse')): ?> // my repeater field

        <img src="<?php the_sub_field('photo'); ?>" class="artists_menu"> // my sub-field, the one I want to get the first row

        <?php endwhile; ?>


        </div>

        <div class="bandeau"></div>

        <h1><?php the_title(); ?></h1>              

        <div class="bandeau"></div>

        </div>


        </div>
        <?php endwhile; ?>
        </a>


        <?php endif; wp_reset_query(); ?>

是照片的 URL,而不是照片本身...这就是为什么我很难获得第一行,但我想保持这种方式。

我在网上找到了这个,但我无法适应我的情况:

<?php

$rows = get_field('repeater_field_name' ); // get all the rows
$first_row = $rows[0]; // get the first row
$first_row_image = $first_row['sub_field_name' ]; // get the sub field value 

// Note
// $first_row_image = 123 (image ID)

$image = wp_get_attachment_image_src( $first_row_image, 'full' );
// url = $image[0];
// width = $image[1];
// height = $image[2];
?>
<img src="<?php echo $image[0]; ?>" />

这是一个带有我的代码的 JSfiddle:

http://jsfiddle.net/5wtRc/

谁能帮我解决这个问题?

非常感谢您的帮助,

【问题讨论】:

    标签: php jquery wordpress parent advanced-custom-fields


    【解决方案1】:

    我刚刚添加了一个简单的布尔值,当检索图像时它设置为 false。然后我将布尔值添加到while 循环的条件中,这样在完成一张图像后,while 循环将失败并继续前进!

    <?php 
    $first_img = true; 
    while($first_img && has_sub_field('photos_presse')): ?> // my repeater field
    
        <img src="<?php the_sub_field('photo'); ?>" class="artists_menu">
        <?php $first_img = false; ?>
    
    <?php endwhile; ?>
    

    希望这会有所帮助!

    【讨论】:

    • 感谢您的回复@Billy Mathews,但它仍然会加载所有图像...您能检查一下来源吗? amsbooking.fr/mattieumoreau/artists
    • 对不起,我没听懂你的问题。您只想加载 1 个帖子? @user2882154
    • 不,我想从所有帖子的转发器字段中加载第一张图片。
    • 我有 7 个帖子。所有帖子都有一个带有 3 或 4 张图像的转发器字段。我想从所有帖子的转发器字段中获取第一张图片。在这里,我得到所有帖子的 3 或 4 张图片。你明白我的意思吗?
    • 啊,好的,1 秒我会改变我的答案
    【解决方案2】:

    @mmdwc 几乎是正确的。由于转发器是一个嵌套字段,因此get_field 不会在您尝试检索子字段时检索任何内容。

    <?php
        $rows = get_sub_field('repeater_field_name' ); // get all the rows of the sub field repeater
        $first_row = $rows[0]; // get the first row of the repeater
        $first_row_image = $first_row['sub_field_name' ]; // get the sub field value of the nested repeater
    ?>
    

    【讨论】:

      猜你喜欢
      • 2020-05-15
      • 2022-07-07
      • 2017-01-10
      • 1970-01-01
      • 2019-03-09
      • 2015-12-04
      • 2021-04-26
      • 2014-05-13
      • 2018-10-06
      相关资源
      最近更新 更多