首页 WordPress函数大全 have_rows()

have_rows()

2020-06-28 / 1325阅 / 悠然

如何你看完本文还不知道如何使用该函数,可以联系我定制视频教程,50元/个函数,学不会全额退款

本视频需要登录以后才能播放

登录

描述

此函数检查父字段(例如Repeater或Flexible Content)是否有任何数据行要循环。这是一个布尔函数,表示它返回truefalse

该功能旨在与the_row()逐步使用可用值结合使用。

使用have_rows()连同the_row()旨在觉得本土很像have_posts()和the_post() WordPress的功能。

参量

have_rows( $selector, [$post_id = false] ); 
  • $selector (字符串) (必填) 字段名称或字段键。
  • $post_id (混合) (可选) 保存值的帖子ID。默认为当前帖子。

返回

(布尔)如果存在一行,则为true。

基本循环

本示例说明如何循环通过称为“ parent_field”的转发器字段并加载子字段值。

if( have_rows('parent_field') ):
    while ( have_rows('parent_field') ) : the_row();
        $sub_value = get_sub_field('sub_field');
        // Do something...
    endwhile;
else :
    // no rows found
endif; 

显示一个滑块。

本示例说明如何循环通过Repeater字段并为基本图像滑块生成HTML。

<?php if( have_rows('slides') ): ?>
    <ul class="slides">
    <?php while( have_rows('slides') ): the_row(); 
        $image = get_sub_field('image');
        ?>
        <li>
            <?php echo wp_get_attachment_image( $image, 'full' ); ?>
            <p><?php the_sub_field('caption'); ?></p>
        </li>
    <?php endwhile; ?>
    </ul>
<?php endif; ?> 

显示布局。

本示例说明如何循环显示“灵活内容”字段并为不同的布局生成HTML。

<?php if( have_rows('content') ): ?>
    <?php while( have_rows('content') ): the_row(); ?>
        <?php if( get_row_layout() == 'paragraph' ): ?>
            <?php the_sub_field('paragraph'); ?>
        <?php elseif( get_row_layout() == 'image' ): 
            $image = get_sub_field('image');
            ?>
            <figure>
                <?php echo wp_get_attachment_image( $image['ID'], 'full' ); ?>
                <figcaption><?php echo $image['caption']; ?></figcaption>
            </figure>
        <?php endif; ?>
    <?php endwhile; ?>
<?php endif; ?> 

嵌套循环

本示例说明如何遍历嵌套的Repeater字段。

<?php 

/**
 * Field Structure:
 *
 * - locations (Repeater)
 *   - title (Text)
 *   - description (Textarea)
 *   - staff_members (Repeater)
 *     - image (Image)
 *     - name (Text)
 */
if( have_rows('locations') ): ?>
    <div class="locations">
    <?php while( have_rows('locations') ): the_row(); ?>
        <div class="location">
            <h3><?php the_sub_field('title'); ?></h3>
            <p><?php the_sub_field('description'); ?></p>
            <?php if( have_rows('staff_members') ): ?>
                <ul class="staff-members">
                <?php while( have_rows('staff_members') ): the_row();
                    $image = get_sub_field('image');
                    ?>
                    <li>
                        <?php echo wp_get_attachment_image( $image['ID'], 'full' ); ?>
                        <h4><?php the_sub_field('name'); ?></h4>
                    </li>
                <?php endwhile; ?>
                </ul>
            <?php endif; ?>
        </div>
    <?php endwhile; ?>
    </div>
<?php endif; ?> 

笔记

无限循环

由于该have_rows()函数本身不会单步执行每一行,因此不使用此函数the_row()将创建无限循环,从而导致白屏。

范围

have_rows()循环的范围仅限于当前行。这意味着任何子字段函数(例如get_sub_field()或)the_sub_field()只能从当前行而不是父行或子行中查找数据。

大家谈论
    我的见解