从循环内的转发器字段获取第一行

Car*_*rth 1 php wordpress loops advanced-custom-fields

我正在使用高级自定义字段制作WP网站.在一个页面上,我想回显一个包含所有类别为"Studio"的帖子的循环.然后我想显示名为"studio_project_dia_img"的子字段中的第一个图像.我可以从repeater_field获取所有图像,但不仅仅是第一个

此代码有效(仅响应来自转发器字段的所有行):

    <div <?php post_class() ?> id="post-<?php the_ID(); ?>">

        <li class="thumbs">
            <a href="studio-single.html" class="clearfix">
            <?php if (get_field('studio_project_dia')) : while(has_sub_field('studio_project_dia')): ?>

                    <img src="<?php echo get_sub_field('studio_project_dia_img', $post->ID); ?>" />
            <?php endwhile; endif; ?>
                    <div class="thumbsText">
                        <h2><?php the_title(); ?></h2>
                        <h3><?php the_tags( 'Tags: ', '&nbsp;/&nbsp; ', ''); ?></h3>
                    </div>
            </a>



        </div>

        <?php edit_post_link('Edit this entry','','.'); ?>

    </div>

<?php endwhile; endif; ?>
Run Code Online (Sandbox Code Playgroud)

这段代码没有:

    <div <?php post_class() ?> id="post-<?php the_ID(); ?>">

        <li class="thumbs">
            <a href="studio-single.html" class="clearfix">
            <?php if (get_field('studio_project_dia')) : while(has_sub_field('studio_project_dia')): ?>
                    <?php
                        $rows = get_sub_field('studio_project_dia_img', $post->ID);
                        $first_row = $rows[0];
                        $first_row_image = $first_row['studio_project_dia_img'];

                        $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] ?>" />
            <?php endwhile; endif; ?>
                    <div class="thumbsText">
                        <h2><?php the_title(); ?></h2>
                        <h3><?php the_tags( 'Tags: ', '&nbsp;/&nbsp; ', ''); ?></h3>
                    </div>
            </a>



        </div>

        <?php edit_post_link('Edit this entry','','.'); ?>

    </div>

<?php endwhile; endif; ?>
Run Code Online (Sandbox Code Playgroud)

doy*_*y44 5

在您的第一个代码中,break;在'img'标记之后添加如下:

<?php if (get_field('studio_project_dia')) : while(has_sub_field('studio_project_dia')): ?>
    <img src="<?php echo get_sub_field('studio_project_dia_img', $post->ID); ?>" />
<?php break; endwhile; endif; ?>
Run Code Online (Sandbox Code Playgroud)

随着break,while循环将停止.