has_sub_field()

Overview

This function is outdated. Please use the have_rows() function instead.

Description

This function is used in a “while loop” to loop through each row of  a repeater field / flexible content field and instantiate it for use with these functions:

Parameters

<?php has_sub_field($field_name, $post_id); ?>
  • $field_name: the name of the repeater field / flexible content field to loop through. eg “gallery_images” (required)
  • $post_id: Specific post ID where your value was entered. Defaults to current post ID (not required). This can also be options / taxonomies / users / etc

Usage

<?php

/*
*  Loop through a Repeater field
*/

if( get_field('repeater') ): ?>

	<?php while( has_sub_field('repeater') ): ?>

		<div>
			<img src="<?php the_sub_field('image'); ?>" alt="<?php the_sub_field('alt'); ?>" />
		    <p><?php the_sub_field('content'); ?></p>
		</div>

	<?php endwhile; ?>

<?php endif; 

/*
*  Loop through a Flexible Content field
*/

if( get_field('flexible_content') ): ?>

	<?php while( has_sub_field("flexible_content") ): ?>

		<?php if(get_row_layout() == "paragraph"): // layout: Paragraph ?>

			<div>
				<?php the_sub_field("content"); ?>
			</div>

		<?php elseif(get_row_layout() == "file"): // layout: File ?>

			<div>
				<a href="<?php the_sub_field("file"); ?>" ><?php the_sub_field("name"); ?></a>
			</div>

		<?php elseif(get_row_layout() == "featured_posts"): // layout: Featured Posts ?>

			<div>
				<h2><?php the_sub_field("title"); ?></h2>
				<?php the_sub_field("content"); ?>

				<?php if(get_sub_field("posts")): ?>
					<ul>
					<?php foreach(get_sub_field("posts") as $p): ?>
						<li><a href="<?php echo get_permalink($p->ID); ?>"><?php echo get_the_title($p->ID); ?></a></li>
					<?php endforeach; ?>
					</ul>
				<?php endif; ?>

			</div>

		<?php endif; ?>

	<?php endwhile; ?>
<?php endif; 

/*
*  Loop through nested Repeater fields
*/

if( get_field('parent_repeater') ): ?>
	<?php while( has_sub_field('parent_repeater') ): ?>

		<div>
			<?php if( get_sub_field('child_repeater') ): ?>
				<ul>
				<?php while( has_sub_field('child_repeater') ): ?>
					<li><?php the_sub_field('item'); ?></li>
				<?php endwhile; ?>
				</ul>
			<?php endif; ?>
		</div>	

	<?php endwhile; ?>
<?php endif; 

/*
*  Loop through nested Repeater fields (From another $post ID)
*/

if( get_field('parent_repeater', 123) ): ?>
	<?php while( has_sub_field('parent_repeater', 123) ): ?>

		<div>
			<?php 

			/*
			*  note: you don't need to specify the $post_id for any sub field functions
			*/

			if( get_sub_field('child_repeater') ): ?>
				<ul>
				<?php while( has_sub_field('child_repeater') ): ?>
					<li><?php the_sub_field('item'); ?></li>
				<?php endwhile; ?>
				</ul>
			<?php endif; ?>
		</div>	

	<?php endwhile; ?>
<?php endif;

?>

Related Documents

Related

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.