Home Forums Front-end Issues Modularise flexible content with one template file per layout


Modularise flexible content with one template file per layout

  • 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(),


    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')) {
        get_template_part('layouts/layout', get_row_layout());
  • Thank you, that was exactly what I was looking for!

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

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.