get_template_part()
get_template_part() es la forma correcta de incluir fragmentos de plantilla en un tema. A diferencia de include() o require(), respeta la jerarquía de tema hijo: busca primero en el tema hijo y luego en el padre.
Sintaxis
get_template_part( $slug, $name, $args );
Cómo construye el nombre del archivo
// Solo slug → busca: template-parts/card.php
get_template_part( 'template-parts/card' );
// Slug + name → busca primero: template-parts/card-producto.php
// si no existe: template-parts/card.php
get_template_part( 'template-parts/card', 'producto' );
Pasar datos al fragmento (WordPress 5.5+)
// En archive-producto.php
while ( have_posts() ) : the_post();
get_template_part( 'template-parts/card', 'producto', [
'mostrar_precio' => true,
'tamaño_imagen' => 'medium',
'post_id' => get_the_ID(),
]);
endwhile;
// En template-parts/card-producto.php
// Los datos llegan en la variable $args
$mostrar_precio = $args['mostrar_precio'] ?? false;
$tamaño = $args['tamaño_imagen'] ?? 'thumbnail';
?>
<article class="card">
<?php the_post_thumbnail( $tamaño ); ?>
<h3><?php the_title(); ?></h3>
<?php if ( $mostrar_precio ) : ?>
<span class="precio"><?php echo esc_html( get_post_meta( get_the_ID(), '_precio', true ) ); ?></span>
<?php endif; ?>
</article>
Estructura de carpetas recomendada
mi-tema/
├── template-parts/
│ ├── card.php ← card genérica
│ ├── card-producto.php ← variante para productos
│ ├── card-noticia.php ← variante para noticias
│ ├── hero.php
│ └── pagination.php
├── single.php
├── archive.php
└── index.php
