Performance: Template parts
Template parts affect page load speed by controlling how reusable sections of a page are loaded and rendered.
<?php
ob_start();
get_template_part('header');
$header_html = ob_get_clean();
echo $header_html;
echo $header_html;
echo $header_html;
?><?php get_template_part('header'); ?> <?php get_template_part('header'); ?> <?php get_template_part('header'); ?>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Repeated get_template_part calls | No extra DOM nodes but slower HTML generation | No direct reflows but delayed HTML delivery | Normal paint cost | [X] Bad |
| Single get_template_part call with output reuse | No extra DOM nodes, faster HTML generation | No reflows, faster HTML delivery | Normal paint cost | [OK] Good |
get_template_part() in WordPress themes?get_template_part() is designed to include reusable parts of a theme, like headers or footers.get_template_part() takes two string arguments: slug and optional name.get_template_part('content', 'single'); with both parameters as strings.get_template_part('header');{slug}.php file.header.php.get_template_part('footer', single);single is treated as a constant or variable, causing syntax error.{slug}-{name}.php in theme root.get_template_part('sidebar', 'left'); loads sidebar-left.php.Pass the test and this pattern moves unknown