LESSON 01 / 04

Template Tags & Template Parts

Split templates into reusable parts with get_header, get_footer and get_template_part, and use the right template tags (and escaping) inside them.

๐ŸŽ“Mediumlevel
โฑ๏ธ30 minto finish
โœ๏ธ1activities

๐Ÿ“‚ Building a Classic Theme๐Ÿท๏ธ WordPress

๐ŸŽฏ By the end of this lesson you canโ€ฆ

  • Split a theme into header, footer and parts
  • Reuse a card with get_template_part
  • Know "the_" vs "get_the_" tags
๐Ÿ˜ PHP single.php ๐Ÿ‘† Tap a line to explain it
1<?php get_header(); ?>2<main>3  <?php while ( have_posts() ) : the_post(); ?>4    <?php get_template_part( 'template-parts/content', get_post_type() ); ?>5  <?php endwhile; ?>6</main>7<?php get_footer(); ?>
๐Ÿ˜ PHP template-parts/content.php
1<article <?php post_class( 'card' ); ?>>2  <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>3  <p class="meta"><?php echo esc_html( get_the_date() ); ?></p>4  <?php the_excerpt(); ?>5</article>
print vs return
PrintsReturns
the_title()get_the_title()
the_permalink()get_permalink()
the_excerpt()get_the_excerpt()
the_date()get_the_date()
๐Ÿ”Œ WordPress PHP print-vs-return.php ๐Ÿ‘† Tap a line to explain it
1<?php2the_post();3$title = get_the_title();4echo strtoupper( $title ), "\n";5the_title( '<h2>', "</h2>\n" );
๐Ÿ’ก Concept check
You want to shorten a post title before printing it. Which function?

๐Ÿ“Œ Key takeaways

  • get_header() loads header.php; get_template_part( 'parts/card' ) loads parts/card.php.
  • the_title() prints; get_the_title() returns (so you can escape or change it).
  • Keep every template tiny: one job each.

Finished reading & practising?

Saved in this browser. Create a free account to keep it forever.