Skip to content

Instantly share code, notes, and snippets.

@KreigD
Created April 20, 2023 19:05
Show Gist options
  • Save KreigD/5f260ed800c7917ed225753a5ea2894e to your computer and use it in GitHub Desktop.
Save KreigD/5f260ed800c7917ed225753a5ea2894e to your computer and use it in GitHub Desktop.
A template for displaying Advanced Custom Fields flexible content rows programatically without having to list each row individually.
<?php
/**
* Partial template for ACF flexible content
*
* @package Understrap
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
the_title( '<h1 class="entry-title d-none">', '</h1>' );
// check for ACF flexible content data
if ( have_rows( 'modules' ) ) {
// loop through the select ACF flexible content layouts
while( have_rows( 'modules' ) ) {
the_row();
// dispaly the matching flexible content partial
get_template_part( 'modules/' . get_row_layout() );
// Just add your row templates into a directory named `modules` in the root of your theme
// Each row template shoud have a filename matching the ACF slug
// (ex: three-columns.php for a field with slug `three-columns`)
}
}
<?php
/**
* Template Name: Flexible Content
*
* Template for displaying ACF Flexible Content Modules
*
* @package Understrap
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
get_header();
?>
<div class="wrapper" id="page-wrapper">
<div class="container-fluid" id="content" tabindex="-1">
<div class="row">
<main class="site-main" id="main">
<?php
while ( have_posts() ) {
the_post();
// Get the template part for looping through our flexible content fields
get_template_part( 'loop-templates/content', 'flexible' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
}
?>
</main><!-- #main -->
</div><!-- .row -->
</div><!-- #content -->
</div><!-- #page-wrapper -->
<?php
get_footer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment