Build classic and block WordPress themes from scratch: the template hierarchy, The Loop, the_title() and the_content(), template parts, menus, the Customizer, child themes and theme.json.
Lessons
The Loop is the block of PHP that walks through the posts WordPress already fetched for the current page and prints each one — every template that shows content is built around it.
Remember from lesson 1 that WordPress runs the database query before your template loads. Your template does not fetch posts. It only loops over what is already there.
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>
</article>
<?php endwhile; ?>
<?php else : ?>
<p><?php esc_html_e( 'Sorry, no posts matched your criteria.', 'gdc-starter' ); ?></p>
<?php endif; ?>| Code | What it does |
|---|---|
have_posts() | Returns true if there are posts left to show in the main query. |
while ( have_posts() ) | Keeps looping while posts remain. |
the_post() | Moves to the next post and sets up the global $post so template tags know which post they are describing. Forget it and you get an infinite loop. |
the_ID(), the_title() … | Template tags. Each prints one piece of the current post. |
else | Runs when the query found nothing — a search with no results, an empty category. |
Watch out: Never omitthe_post(). Without it,have_posts()stays true forever and the page loops until PHP runs out of memory.
You will notice while ( ... ) : and endwhile; instead of curly braces. Both work identically. The colon form is preferred in templates because a lone } among dozens of lines of HTML is impossible to match by eye, while endwhile; and endif; say exactly what they close.
The Loop reads from the main query, which WordPress built from the URL:
| URL | The main query contains |
|---|---|
/ | The latest posts (or the front page) |
/category/news/ | Posts in the News category |
/2026/10/hello-world/ | One post — the Loop runs once |
/?s=theme | Posts matching the search "theme" |
The same Loop code works on every one of them. That is why a single index.php can power an entire site.
The global $wp_query knows where the Loop is. Use it to style the first post differently or add a divider between items:
<?php while ( have_posts() ) : the_post(); ?>
<?php
global $wp_query;
$is_first = ( 0 === $wp_query->current_post );
?>
<article <?php post_class( $is_first ? 'post--featured' : '' ); ?>>
<?php the_title( '<h2 class="entry-title">', '</h2>' ); ?>
</article>
<?php endwhile; ?>post_class() accepts extra class names and merges them with the ones WordPress generates — such as post, type-post, status-publish, category-news and has-post-thumbnail — which gives you hooks for CSS without any extra PHP.
query_posts() inside a template to change the posts. It replaces the main query, breaks pagination and is discouraged. Use pre_get_posts (lesson 21) or a separate WP_Query.$post->post_title directly. It skips the filters plugins rely on. Use the_title() or get_the_title().wp_reset_postdata() — covered in lesson 21.index.php and confirm your imported test posts appear.else message shows.post--featured class to the first post only and style it in style.css.In accordance with Section 6 & Section 7 of the DPDP Act 2023, third-party analytics and marketing trackers are strictly gated behind your affirmative consent. You may withdraw consent at any time under Section 6(4).
Essential for session authentication (ci_session), Cross-Site Request Forgery security (csrf_cookie_name), and Cloudflare DDoS/bot prevention. Cannot be disabled.
Gated under DPDP Section 7. Allows Google Analytics 4 (Google Consent Mode v2) to measure aggregate, anonymized tool usage.
Enables browser local storage to preserve your theme selection (dark/light mode) and code editor layout between visits.
Used to display developer sponsorships and native tool partner notices.