Hi.
I’m looking for some tips for how I can modularise my frontend code a bit. I’m using flexible fields to build a “page builder” of some kind. I would like to split my template for the frontend, since it becomes quite overwhelming when having many layouts with lot’s of fields, repeaters etc.
I would like to loop through and call a template for each layout. Have anyone done something like this and do you have any tips or suggestions?
Yes, use get_template_part(),
example:
Create a flex field with 2 layouts named “layout1” and “layout2”.
Create a folder in your theme named “layouts” …wp-content/themes/your-theme/layouts
In this folder create 2 files layout-layout1.php and layout-layout2.php
if (have_rows('flex_field')) {
while (have_rows('flex_field')) {
the_row();
get_template_part('layouts/layout', get_row_layout());
}
}
Thank you, that was exactly what I was looking for!