Building a custom WordPress theme: files, the Loop, and publishing

WordPress is an open-source CMS written in PHP that stores content in MySQL (or MariaDB). A theme controls how that content is rendered. These are my notes from building a fairly rough custom theme, bluey-wordpress-theme, styled with Tailwind CSS.
Classic themes vs. block themes
Section titled “Classic themes vs. block themes”The theme in this post is a classic theme: PHP template files that WordPress picks using the template hierarchy. Since WordPress 5.9, WordPress also supports block themes, which use HTML templates and a theme.json file and are edited in the Site Editor. Classic themes still work. If you’re starting a new theme today, read the block theme docs first.
Files in a classic theme
Section titled “Files in a classic theme”Only two files are required: style.css and index.php. The rest are optional, and WordPress falls back to index.php when a more specific template is missing.
style.css: the header comment holds the theme metadata (Theme Name, Author, Version, Text Domain, and so on).index.php: the fallback template for every request.header.php/footer.php/sidebar.php: shared parts, loaded withget_header(),get_footer()andget_sidebar().functions.php: registers menus, widget areas, theme supports (such aspost-thumbnails), and enqueues scripts and styles.single.php,page.php,archive.php: templates for single posts, pages and archive listings.template-parts/: reusable chunks loaded withget_template_part().
The workflow
Section titled “The workflow”- Run WordPress locally (Docker Compose with the official
wordpressandmysqlimages works well) and create a folder underwp-content/themes/. - Add
style.cssandindex.php, then activate the theme in Appearance → Themes. - Add
header.php,footer.php,functions.php, and the specific templates as you need them. Use template tags (the_title(),the_content(),the_permalink()) to output post data. - Turn on
WP_DEBUGinwp-config.phpwhile developing so PHP notices and deprecations show up. - Test on a few screen sizes and with real content: long titles, posts without featured images, posts with no comments.
The post-card Loop
Section titled “The post-card Loop”This is the Loop from my theme’s listing template. It renders each post as a Tailwind card with the featured image, title, excerpt, author, date, an edit link, and a comment link. Some of the translation strings still use the html5blank text domain.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" class="inline-block max-w-sm rounded overflow-hidden shadow-lg"> <?php if ( has_post_thumbnail() ) : ?> <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> <img class="w-full" src="<?php the_post_thumbnail_url(); ?>" alt="<?php the_title_attribute(); ?>"> </a> <?php endif; ?>
<div class="px-6 py-4"> <div class="font-bold text-xl mb-2"><?php the_title(); ?></div> <div class="text-gray-700 text-base"> <?php the_excerpt(); ?> </div> </div>
<div class="px-6 py-4"> <span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2"> <?php _e( 'Published by', 'html5blank' ); ?> <?php the_author_posts_link(); ?> </span> <span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2"> <?php the_time( 'F j, Y g:i a' ); ?> </span> <?php edit_post_link( __( 'Edit', 'html5blank' ), '<span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700 mr-2">', '</span>' ); ?> <?php if ( comments_open() ) : ?> <span class="inline-block bg-gray-200 rounded-full px-3 py-1 text-sm font-semibold text-gray-700"> <?php comments_popup_link( __( 'Leave your thoughts', 'html5blank' ), __( '1 Comment', 'html5blank' ), __( '% Comments', 'html5blank' ) ); ?> </span> <?php endif; ?> </div> </div>
<?php endwhile; else : ?>
<article> <h2><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h2> </article>
<?php endif; ?>have_posts() / the_post() iterate over the main query and set up the global post, so template tags like the_title() and the_ID() refer to the current post. If the query is empty, the else branch prints a translatable “nothing to display” message.
What changed from the first version
Section titled “What changed from the first version”- Unescaped titles in attributes. The first version put
the_title()insidetitle="…"andalt="…". A title containing a quote breaks the markup.the_title_attribute()escapes the value for use in an attribute. <p>aroundthe_excerpt().the_excerpt()already wraps its output in<p>, so the extra<p>produced invalid nested paragraphs. A<div>avoids that.- Empty edit pill.
edit_post_link()prints nothing for visitors who can’t edit the post, but the surrounding<span>still rendered as an empty grey pill. Passing the<span>as the$before/$afterarguments means it only appears when the link does. - Hidden comment link. The original added a
hiddenclass when the post had zero comments, which meant the “Leave your thoughts” text could never be seen. Wrapping the pill incomments_open()shows it whenever comments are allowed. - Stray markup. Three
<br />tags inside the Loop added spacing per card. That belongs in CSS (Tailwind margin classes).
Publishing a theme
Section titled “Publishing a theme”- WordPress.org Theme Directory: free themes only. Themes must be GPL-compatible and pass a manual review against the theme requirements. The text domain must match the theme slug, so a theme that still uses another starter theme’s text domain (like
html5blankabove) will be flagged. Run the Theme Check plugin before submitting. - Marketplaces such as ThemeForest: paid themes, with their own review and a commission on sales.
- Your own site or GitHub: zip the theme folder so users can upload it through Appearance → Themes → Add New Theme → Upload Theme. GitHub also lets others file issues and send pull requests.
References
Section titled “References”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)