๐ฏ 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>
| Prints | Returns |
|---|---|
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.