Skip to content

The sek_render_sections() template tag

sek_render_sections() is the one call a theme template needs to print a page’s sections. It returns the sections as HTML, wrapped in a single <div data-sek-sections>, and it works in both render modes because it renders through the same template resolution as every other render path.

sek_render_sections(?int $postId = null): string
  • It returns the markup and does not print it. Echo the result yourself: echo sek_render_sections();.
  • $postId is the post to render. Leave it out inside The Loop to render the current post (it defaults to get_the_ID()), or pass an ID to render another post’s sections.
  • The wrapper is <div data-sek-sections>. Every top-level section sits inside it as a direct child. The data-sek-sections attribute is what the live preview client uses to find where sections live in the DOM.

Outside live preview, sek_render_sections() returns '' when the post has no SEK data or no section to render. It also returns '' when the post does not exist, when the post type has no SEK config, and when the plugin has not finished loading.

Returning '' instead of an empty wrapper lets a template fall back to the post’s regular content. The recommended shape is:

$sections = sek_render_sections();
if ($sections !== '') {
echo $sections;
} else {
the_content();
}

A page that was never edited in SEK, or a post type SEK does not manage, then renders the way it always did. The starter page template that ships with the plugin (sek-starter-template.php, written into your theme by Install demo sections) is a complete example. It guards the call with function_exists('sek_render_sections'), so the theme keeps working if the plugin is ever deactivated:

get_header();
while (have_posts()) :
the_post();
$sek_starter_sections = function_exists('sek_render_sections') ? sek_render_sections() : '';
if ($sek_starter_sections !== '') {
echo $sek_starter_sections;
} else {
the_content();
}
endwhile;
get_footer();

Live preview works through the template the editor’s full-page preview renders, so the tag previews live only when it sits in one of those templates. The preview renders a fixed candidate list, the first one the theme has:

  • Classic themes, pages: the page template assigned to the page, then page.php, singular.php, index.php.
  • Classic themes, other post types: single.php, singular.php, index.php.
  • Blade (Sage) themes: the page view for pages (single for other post types), then singular, then index. Assigned page templates are not used.

Any other template in the hierarchy, such as front-page.php, page-{slug}.php, home.php or single-{type}.php, is never what the preview renders. If you call the tag from front-page.php, call it from page.php too (or assign a page template that calls it) so the front page previews live. Within a candidate template the theme needs no extra wiring for the sections the tag prints.

During a preview request, each top-level section’s root element carries the data-section-id marker the editor uses to swap that section in place. That marker arrives through $sekAttr, the same variable every section template already prints (see Handles & Blade templates). Outside preview $sekAttr is an empty string, so the live page carries no data-section-id markers.

In preview the empty-result rule changes: a page with no sections still returns the empty <div data-sek-sections></div> wrapper, so the preview client has a container to insert a first section into.

  • Every top-level section instance, once, by its own template. The tag does not descend into a section’s nested sections or children fields. Rendering those is the instance template’s job, which is why a template that holds nested sections prints them itself.
  • Global sections through their wrapper. A global section placed on the page resolves to its member sections, and each is rendered by its own template without the preview marker.
  • Errors in place. A section whose template path is disallowed, or whose template throws, renders the inline sek-preview-error div where that section would be. The rest of the page still renders.

Differences from a hand-written theme loop

Section titled “Differences from a hand-written theme loop”

If your theme already loops over the stored sections itself (as starterkit-classic and a Sage theme’s sections partial do), the tag renders the same output with three deliberate differences. Each follows the rule that the current config is the truth about what a page holds, the same rule the recursive render walk applies.

  • Orphan instances are skipped. An instance whose handle is no longer in the config is dropped silently. A hand-written loop that reads the stored templatePath still renders it.
  • The config’s template path wins. The templatePath from the current config for that handle takes precedence, and the path stored on the instance is only the fallback. A hand-written loop uses the stored path alone.
  • A missing file reads differently. An empty or disallowed path gives Template "X" not found or disallowed. An allowed path whose file does not exist gives Template render failed: ... with the reason. The div’s tag, class, style, and marker are the same either way.