the_flexible_field()

Overview

This function has been deprecated. That means it has been replaced by a new function or is no longer supported, and may be removed from future versions. All code that uses this function should be converted to use its replacement if one exists.

Description

  • Deprecated in v3.3.4 – please replace with has_sub_field
  • Added in v3.1.0

This function is used in a “while loop” to loop through each row of  a flexible content field.  This function requires the flexible content field (activation code required).

This function will return either: the current row (continue loop) or false (end of loop). Within the “while loop”, you can use these functions:

Parameters

<?php the_flexible_field($field_name, $post_id); ?>
  • $field_name: the name of the flexible content field to be retrieved. eg “page_content” (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 Flexible Content field and display it's content with different views for different layouts
*/

while(the_flexible_field("content")): ?>

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

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