A WordPress theme controls the visual presentation and layout of a WordPress site. It consists of template files, stylesheets, functions, and optional assets (JavaScript, images, fonts). Themes define how content stored in the database is rendered in the browser.
A theme does not modify core WordPress functionality; instead, it uses WordPress APIs, hooks, and template hierarchy rules to display content.
Basic Theme Structure
A minimal theme requires:
style.cssindex.php
Example: style.css (Theme Header)
/*
Theme Name: Example Theme
Theme URI: https://example.com
Author: Your Name
Author URI: https://example.com
Description: A minimal custom WordPress theme.
Version: 1.0
License: GNU General Public License v2 or later
Text Domain: example-theme
*/
The header comment identifies the theme in the WordPress admin panel.
Core Theme Files
Common theme files include:
index.php– Fallback templatefunctions.php– Theme functionality and hooksstyle.css– Main stylesheetheader.php– Header templatefooter.php– Footer templatesidebar.php– Sidebar templatesingle.php– Single post templatepage.php– Static page templatearchive.php– Archive template404.php– Not found template
Template Hierarchy
WordPress uses a template hierarchy to determine which file to load.
Example hierarchy for a single post:
single-{posttype}.phpsingle.phpsingular.phpindex.php
For a page:
page-{slug}.phppage-{id}.phppage.phpindex.php
If a more specific template does not exist, WordPress falls back to a more general one.
The Loop
The Loop is used to retrieve and display posts.
Example: Basic Loop
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<div>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php else : ?>
<p>No posts found.</p>
<?php endif; ?>
The Loop relies on the global $wp_query object.
functions.php
The functions.php file allows developers to extend theme functionality.
Enqueue Styles and Scripts
function example_theme_enqueue_assets() {
wp_enqueue_style(
'example-theme-style',
get_stylesheet_uri(),
array(),
'1.0'
);
wp_enqueue_script(
'example-theme-script',
get_template_directory_uri() . '/js/main.js',
array('jquery'),
'1.0',
true
);
}
add_action('wp_enqueue_scripts', 'example_theme_enqueue_assets');
Using wp_enqueue_* functions ensures compatibility and avoids conflicts.
Theme Support Features
Themes should declare support for WordPress features.
function example_theme_setup() {
add_theme_support('title-tag');
add_theme_support('post-thumbnails');
add_theme_support('custom-logo');
add_theme_support('html5', array(
'search-form',
'comment-form',
'comment-list',
'gallery',
'caption'
));
register_nav_menus(array(
'primary' => __('Primary Menu', 'example-theme')
));
}
add_action('after_setup_theme', 'example_theme_setup');