Paramclasses Academy - Blog

Tutorials Details

←
Post 36 of 40
→

WordPress Development with PHP: Template Hierarchy and Theme Files

WordPress Development with PHP: Template Hierarchy and Theme Files

November 17, 2026

1. Introduction

WordPress is built using PHP, MySQL, HTML, CSS and JavaScript. PHP plays an important role because it allows WordPress to retrieve website content from the database and dynamically generate HTML pages.

When developing a WordPress theme, you work with different files such as:

  • index.php

  • header.php

  • footer.php

  • functions.php

  • single.php

  • page.php

  • archive.php

  • style.css

Each file has a specific responsibility.

WordPress also provides a powerful system called Template Hierarchy. It determines which template file should be used when a visitor opens a particular page.

In this tutorial, we will learn:

  1. WordPress theme architecture.

  2. The role of PHP in theme development.

  3. Important WordPress theme files.

  4. Template Hierarchy and template selection.

  5. The WordPress Loop.

  6. Template Tags and Conditional Tags.

  7. How to create a basic custom classic theme.

  8. How to display posts and pages using PHP.

  9. How to load CSS and JavaScript correctly.

  10. Common theme development errors.

By the end, you will understand how WordPress themes work internally and how developers customize them using PHP.

2. What Is WordPress Theme Development?

A WordPress theme controls the visual presentation and layout of a website.

It determines how visitors see:

  • Website header

  • Navigation menu

  • Homepage

  • Blog posts

  • Static pages

  • Sidebar

  • Footer

  • Archive pages

  • Search results

A WordPress theme uses template files to generate the website's HTML output.

For example, when a visitor opens a blog post, WordPress selects an appropriate template file, retrieves the post content and generates the final webpage.

How WordPress Theme Development Works

The basic process is:

  1. A visitor opens a website URL.

  2. WordPress processes the request.

  3. WordPress identifies the requested content.

  4. The Template Hierarchy determines the appropriate template.

  5. PHP executes the selected template.

  6. WordPress retrieves content from the database.

  7. HTML is generated and sent to the browser.

  8. CSS and JavaScript control presentation and interaction.

This is the foundation of dynamic WordPress theme development.

3. Understand the WordPress Theme Directory

Every WordPress theme is stored inside:

wp-content/themes/

For example:

wordpress/
│
└── wp-content/
    │
    └── themes/
        │
        ├── twentytwentyfive/
        │
        └── param-theme/

The param-theme folder will contain our custom theme files.

A basic classic PHP theme can have this structure:

param-theme/
│
├── style.css
│
├── index.php
│
├── functions.php
│
├── header.php
│
├── footer.php
│
├── single.php
│
├── page.php
│
├── archive.php
│
├── search.php
│
├── 404.php
│
└── assets/
    │
    ├── css/
    │   └── custom.css
    │
    └── js/
        └── custom.js

You do not need every optional template file to create a basic classic theme. WordPress can fall back to index.php when a more specific template does not exist.

4. Understand Important WordPress Theme Files

4.1 style.css

This is the main stylesheet of a classic WordPress theme.

It controls:

  • Fonts

  • Colors

  • Spacing

  • Layout

  • Buttons

  • Responsive design

It also contains the theme's metadata header.

Example:

/*
Theme Name: Param Theme
Author: Param Classes
Description: A custom WordPress classic theme.
Version: 1.0
Text Domain: param-theme
*/

body {
    margin: 0;
    font-family: Arial, sans-serif;
    background: #f5f5f5;
    color: #222;
}

h1,
h2,
h3 {
    color: #222;
}

The metadata allows WordPress to recognize the theme.

4.2 index.php

index.php is the main fallback template in a classic theme.

If WordPress cannot find a more specific matching PHP template, it can use index.php.

Example:

<?php get_header(); ?>

<main>

    <h1>Welcome to Param Classes</h1>

    <?php if (have_posts()) : ?>

        <?php while (have_posts()) : the_post(); ?>

            <article>

                <h2>
                    <a href="<?php echo esc_url(get_permalink()); ?>">
                        <?php the_title(); ?>
                    </a>
                </h2>

                <?php the_excerpt(); ?>

            </article>

        <?php endwhile; ?>

    <?php else : ?>

        <p>No content found.</p>

    <?php endif; ?>

</main>

<?php get_footer(); ?>

4.3 header.php

This file contains the common top section of the website.

It generally includes:

  • HTML document structure

  • Head section

  • Site title

  • Navigation

  • Header layout

  • wp_head() hook

Example:

<!DOCTYPE html>
<html <?php language_attributes(); ?>>

<head>

    <meta charset="<?php bloginfo('charset'); ?>">

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <?php wp_head(); ?>

</head>

<body <?php body_class(); ?>>

<?php wp_body_open(); ?>

<header>

    <div class="site-logo">

        <a href="<?php echo esc_url(home_url('/')); ?>">

            <?php bloginfo('name'); ?>

        </a>

    </div>

    <nav>

        <?php
        wp_nav_menu(array(
            'theme_location' => 'primary',
            'container'      => false,
            'fallback_cb'    => false
        ));
        ?>

    </nav>

</header>

Important functions:

  • language_attributes() outputs language attributes for the HTML element.

  • bloginfo('charset') displays the site's character encoding.

  • wp_head() allows WordPress, plugins and themes to add required head elements.

  • body_class() adds useful CSS classes to the body.

  • wp_body_open() provides a hook immediately after the opening body tag.

  • home_url('/') returns the site's homepage URL.

  • wp_nav_menu() displays a registered navigation menu.

4.4 footer.php

This file contains the common bottom section of the website.

Example:

<footer>

    <p>
        Copyright &copy; <?php echo esc_html(wp_date('Y')); ?>
        <?php bloginfo('name'); ?>
    </p>

</footer>

<?php wp_footer(); ?>

</body>

</html>

wp_footer() is important because WordPress and plugins use it to output scripts and other footer content.

4.5 functions.php

This file contains theme-specific PHP functionality.

Developers use it to:

  • Register navigation menus.

  • Enable theme features.

  • Load CSS and JavaScript.

  • Register sidebars.

  • Define custom theme functions.

Example:

<?php

function param_theme_setup() {

    add_theme_support('title-tag');

    add_theme_support('post-thumbnails');

    add_theme_support('html5', array(
        'search-form',
        'comment-form',
        'comment-list',
        'gallery',
        'caption',
        'style',
        'script'
    ));

    register_nav_menus(array(
        'primary' => 'Primary Menu'
    ));

}

add_action('after_setup_theme', 'param_theme_setup');

The after_setup_theme hook is commonly used for theme setup.

4.6 single.php

This file displays an individual blog post.

Example:

<?php get_header(); ?>

<main>

    <?php if (have_posts()) : ?>

        <?php while (have_posts()) : the_post(); ?>

            <article>

                <h1><?php the_title(); ?></h1>

                <?php the_post_thumbnail('large'); ?>

                <div>

                    <?php the_content(); ?>

                </div>

            </article>

        <?php endwhile; ?>

    <?php endif; ?>

</main>

<?php get_footer(); ?>

4.7 page.php

This file displays individual static pages such as:

  • About Us

  • Contact Us

  • Privacy Policy

  • Services

Example:

<?php get_header(); ?>

<main>

    <?php if (have_posts()) : ?>

        <?php while (have_posts()) : the_post(); ?>

            <article>

                <h1><?php the_title(); ?></h1>

                <?php the_content(); ?>

            </article>

        <?php endwhile; ?>

    <?php endif; ?>

</main>

<?php get_footer(); ?>

4.8 archive.php

This file displays archive pages when a more specific archive template is not available.

Archive pages can include:

  • Category archives

  • Tag archives

  • Author archives

  • Date archives

Example:

<?php get_header(); ?>

<main>

    <h1><?php the_archive_title(); ?></h1>

    <?php if (have_posts()) : ?>

        <?php while (have_posts()) : the_post(); ?>

            <article>

                <h2>

                    <a href="<?php echo esc_url(get_permalink()); ?>">

                        <?php the_title(); ?>

                    </a>

                </h2>

                <?php the_excerpt(); ?>

            </article>

        <?php endwhile; ?>

        <?php the_posts_pagination(); ?>

    <?php else : ?>

        <p>No posts found.</p>

    <?php endif; ?>

</main>

<?php get_footer(); ?>

5. What Is WordPress Template Hierarchy?

Template Hierarchy is a system that determines which theme template WordPress loads for a particular request.

WordPress checks the available templates in a specific order.

It uses the most specific matching template available and falls back to a more general template if necessary.

For example, suppose a visitor opens a blog post.

WordPress checks:

single-post-{post-slug}.php
        ↓
single-post.php
        ↓
single.php
        ↓
singular.php
        ↓
index.php

The first matching template is used.

Important Template Hierarchy Files

Template Purpose
front-page.php Website front page
home.php Blog posts index
single.php Individual posts
page.php Individual pages
archive.php General archive
category.php Category archive
tag.php Tag archive
author.php Author archive
search.php Search results
404.php Page not found
index.php Final classic-theme fallback

Important Difference: front-page.php and home.php

These two files are frequently confused.

front-page.php: Controls the website's front page.

home.php: Controls the blog posts index.

For example, if your website uses a static homepage and a separate Blog page:

  • front-page.php can control the static homepage.

  • home.php can control the Blog posts listing.

If front-page.php exists, it takes priority for the front page.

Official documentation: WordPress Template Hierarchy.

6. Understand the WordPress Loop

The WordPress Loop is the PHP mechanism used to retrieve and display posts from the current WordPress query.

It is one of the most important concepts in WordPress theme development.

Basic structure:

<?php

if (have_posts()) :

    while (have_posts()) :

        the_post();

        the_title();

        the_content();

    endwhile;

else :

    echo 'No posts found.';

endif;

?>

Understand Each Function

1. have_posts()

Checks whether the current query has another post available.

2. the_post()

Advances the Loop to the next post and sets up the global post data.

3. the_title()

Displays the current post title.

4. the_content()

Displays the current post content.

5. endwhile

Ends the Loop.

6. else

Runs when the query contains no posts.

7. endif

Ends the conditional statement.

Practical Loop Example

<?php if (have_posts()) : ?>

    <?php while (have_posts()) : the_post(); ?>

        <div class="post-card">

            <h2>
                <?php the_title(); ?>
            </h2>

            <p>
                Published on:
                <?php echo esc_html(get_the_date()); ?>
            </p>

            <?php the_post_thumbnail('medium'); ?>

            <?php the_excerpt(); ?>

            <a href="<?php echo esc_url(get_permalink()); ?>">
                Read More
            </a>

        </div>

    <?php endwhile; ?>

<?php else : ?>

    <p>No posts available.</p>

<?php endif; ?>

This example displays the title, publication date, featured image, excerpt and post link.

7. Understand WordPress Template Tags

Template Tags are WordPress functions that retrieve and display website information inside theme templates.

Important Template Tags:

 
Function Purpose
the_title() Displays post title
the_content() Displays post content
the_excerpt() Displays post excerpt
the_permalink() Displays post permalink
the_post_thumbnail() Displays featured image
the_author() Displays author name
the_date() Displays post date
bloginfo() Displays site information
get_header() Includes header.php
get_footer() Includes footer.php
get_sidebar() Includes sidebar.php

 

 

 

Example

<h1><?php the_title(); ?></h1>

<div class="post-content">

    <?php the_content(); ?>

</div>

<p>

    Author:

    <?php the_author(); ?>

</p>

Template Tags help developers retrieve WordPress data without writing manual database queries.

8. Understand Conditional Tags

Conditional Tags allow developers to check the type of page currently being displayed.

They are useful when you want to display different content on different pages.

Important Conditional Tags:

Function Purpose
is_home() Checks the blog posts index
is_front_page() Checks the front page
is_single() Checks an individual post
is_page() Checks an individual page
is_archive() Checks an archive
is_category() Checks a category archive
is_search() Checks search results
is_404() Checks a not-found page

 

 

Practical Example

<?php

if (is_front_page()) {

    echo 'Welcome to our website!';

} elseif (is_single()) {

    echo 'You are reading a blog post.';

} elseif (is_page()) {

    echo 'You are viewing a website page.';

} elseif (is_archive()) {

    echo 'You are viewing an archive.';

}

?>

Important: Conditional Tags should generally be used after WordPress has established the main query, such as inside a template or an appropriate hook. They may not return the expected result when called too early.

9. Practical Project: Create a Basic Custom WordPress Theme

Now we will create a simple classic PHP theme named Param Theme.

Step 1: Create the Theme Folder

Open:

wp-content/themes/

Create a new folder:

param-theme

Step 2: Create style.css

Inside param-theme, create style.css.

Add:

/*
Theme Name: Param Theme
Author: Param Classes
Description: A beginner-friendly custom WordPress theme.
Version: 1.0
Text Domain: param-theme
*/

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: Arial, sans-serif;
    background: #f4f6f8;
    color: #222;
}

header {
    background: #172554;
    color: white;
    padding: 20px 8%;
}

header a {
    color: white;
    text-decoration: none;
}

nav ul {
    display: flex;
    gap: 20px;
    list-style: none;
    padding: 0;
}

main {
    max-width: 1100px;
    margin: 30px auto;
    padding: 20px;
}

.post-card {
    background: white;
    margin-bottom: 20px;
    padding: 20px;
    border-radius: 8px;
}

.post-card img {
    max-width: 100%;
    height: auto;
}

footer {
    background: #172554;
    color: white;
    text-align: center;
    padding: 20px;
}

Step 3: Create functions.php

Create functions.php and add:

<?php

function param_theme_setup() {

    add_theme_support('title-tag');

    add_theme_support('post-thumbnails');

    register_nav_menus(array(

        'primary' => 'Primary Menu'

    ));

}

add_action('after_setup_theme', 'param_theme_setup');


function param_theme_assets() {

    wp_enqueue_style(
        'param-theme-style',
        get_stylesheet_uri(),
        array(),
        '1.0'
    );

}

add_action('wp_enqueue_scripts', 'param_theme_assets');

This code enables theme features, registers a menu location and loads the main stylesheet through WordPress.

Step 4: Create header.php

<!DOCTYPE html>
<html <?php language_attributes(); ?>>

<head>

    <meta charset="<?php bloginfo('charset'); ?>">

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <?php wp_head(); ?>

</head>

<body <?php body_class(); ?>>

<?php wp_body_open(); ?>

<header>

    <h2>

        <a href="<?php echo esc_url(home_url('/')); ?>">

            <?php bloginfo('name'); ?>

        </a>

    </h2>

    <nav>

        <?php

        wp_nav_menu(array(

            'theme_location' => 'primary',

            'container' => false,

            'fallback_cb' => false

        ));

        ?>

    </nav>

</header>

Step 5: Create footer.php

<footer>

    <p>

        Copyright &copy;

        <?php echo esc_html(wp_date('Y')); ?>

        <?php bloginfo('name'); ?>

    </p>

</footer>

<?php wp_footer(); ?>

</body>

</html>

Step 6: Create index.php

<?php get_header(); ?>

<main>

    <h1>Latest Blog Posts</h1>

    <?php if (have_posts()) : ?>

        <?php while (have_posts()) : the_post(); ?>

            <article class="post-card">

                <h2>

                    <a href="<?php echo esc_url(get_permalink()); ?>">

                        <?php the_title(); ?>

                    </a>

                </h2>

                <p>

                    Published:

                    <?php echo esc_html(get_the_date()); ?>

                </p>

                <?php

                if (has_post_thumbnail()) {

                    the_post_thumbnail('medium');

                }

                ?>

                <?php the_excerpt(); ?>

                <a href="<?php echo esc_url(get_permalink()); ?>">

                    Read More

                </a>

            </article>

        <?php endwhile; ?>

        <?php the_posts_pagination(); ?>

    <?php else : ?>

        <p>No posts found.</p>

    <?php endif; ?>

</main>

<?php get_footer(); ?>

Step 7: Activate the Theme

Open your WordPress dashboard.

Navigate to:

Appearance → Themes

You should see:

Param Theme

Click Activate.

Your custom classic theme is now active.

Step 8: Add Sample Blog Posts

Navigate to:

Dashboard → Posts → Add New Post

Create three sample posts:

  1. Introduction to WordPress.

  2. PHP Programming for Beginners.

  3. WordPress Theme Development.

Publish the posts and open your website frontend.

You should see the blog post titles, dates, excerpts and featured images if assigned.

10. Load Custom CSS and JavaScript Correctly

WordPress developers should use the enqueue system instead of manually adding stylesheet and script tags to templates.

Load a Custom CSS File

Create:

assets/css/custom.css

Add:

body {
    line-height: 1.6;
}

.post-card {
    border: 1px solid #ddd;
}

Then update functions.php:

function param_theme_assets() {

    wp_enqueue_style(
        'param-theme-style',
        get_stylesheet_uri(),
        array(),
        '1.0'
    );

    wp_enqueue_style(
        'param-custom-style',
        get_theme_file_uri('/assets/css/custom.css'),
        array('param-theme-style'),
        '1.0'
    );

}

add_action('wp_enqueue_scripts', 'param_theme_assets');

Load a Custom JavaScript File

Create:

assets/js/custom.js

Add:

document.addEventListener('DOMContentLoaded', function () {

    console.log('Param Theme JavaScript Loaded');

});

Add this to the same param_theme_assets() function:

wp_enqueue_script(
    'param-theme-script',
    get_theme_file_uri('/assets/js/custom.js'),
    array(),
    '1.0',
    true
);

The final true requests that the script be placed in the footer.

Official documentation: Including CSS and JavaScript.

11. Understand the Difference Between Classic Themes and Block Themes

WordPress supports two major theme development approaches.

Classic Themes

Classic themes use PHP template files.

Examples:

  • index.php

  • header.php

  • footer.php

  • single.php

  • page.php

Developers use PHP Template Tags, The Loop and the PHP Template Hierarchy.

Block Themes

Block themes use HTML block templates and template parts.

Examples:

  • templates/index.html

  • templates/single.html

  • templates/page.html

  • parts/header.html

  • parts/footer.html

  • theme.json

Block themes are designed for editing layouts through the Site Editor.

Main Differences

Classic Theme Block Theme
PHP template files HTML block templates
PHP Template Tags Block markup
header.php parts/header.html
footer.php parts/footer.html
PHP-based template editing Site Editor support
style.css style.css and commonly theme.json

 

The Template Hierarchy concept applies to both approaches, although their template file extensions and template systems differ.

For this tutorial, we are focusing on classic PHP theme development.

12. Common WordPress Theme Development Errors

Error 1: Theme Is Not Showing in Appearance → Themes

Possible reasons:

  • Theme folder is in the wrong location.

  • style.css is missing.

  • Theme metadata is incorrect.

  • File permissions prevent WordPress from reading the theme.

Solution: Verify that style.css and index.php exist in the theme's root directory.

Error 2: Header Is Not Displaying

Possible reasons:

  • header.php is missing.

  • get_header() is not included.

  • PHP syntax error exists.

Solution: Verify the header filename and check your template code.

Error 3: CSS Is Not Loading

Possible reasons:

  • Incorrect stylesheet path.

  • Enqueue function is not running.

  • Browser cache is displaying an older file.

Solution: Check wp_enqueue_style(), the file path and browser developer tools.

Error 4: Blog Posts Are Not Displaying

Possible reasons:

  • No posts are published.

  • The current query returns no results.

  • The Loop contains incorrect conditions.

Solution: Verify published posts and use have_posts() and the_post() correctly.

Error 5: Footer Scripts Are Not Working

Possible reason: The wp_footer() hook is missing from the theme.

Solution: Add wp_footer() immediately before the closing body tag in footer.php.

Error 6: Wrong Template Is Loading

Possible reasons:

  • Template filename is incorrect.

  • A more specific template exists.

  • A child theme or saved template is overriding the expected template.

Solution: Check the Template Hierarchy and inspect the active theme's files.

13. Best Practices for WordPress Theme Developers

  1. Follow WordPress coding standards.

  2. Use the correct Template Hierarchy filenames.

  3. Keep reusable header and footer sections separate.

  4. Use functions.php for theme setup and theme-specific functionality.

  5. Use wp_enqueue_style() and wp_enqueue_script().

  6. Escape dynamic output using functions such as esc_html(), esc_attr() and esc_url() according to the output context.

  7. Sanitize and validate incoming data.

  8. Use WordPress APIs instead of directly querying the database when possible.

  9. Avoid hardcoding website URLs.

  10. Use child themes when customizing an existing parent theme.

  11. Make themes responsive and accessible.

  12. Keep plugin functionality separate from presentation-related theme functionality.

  13. Test templates with different content types.

  14. Use WP_DEBUG during development and disable public error display on production websites.

14. Practical Assignment

Project Name: Param Classes Custom Blog Theme

Create a custom classic WordPress theme using PHP.

Requirements:

  1. Create a theme folder named param-classes-theme.

  2. Create style.css.

  3. Create index.php.

  4. Create functions.php.

  5. Create header.php.

  6. Create footer.php.

  7. Create single.php.

  8. Create page.php.

  9. Create archive.php.

  10. Register a primary navigation menu.

  11. Enable featured images.

  12. Load CSS using wp_enqueue_style().

  13. Display blog posts using The Loop.

  14. Display post title, date, featured image and excerpt.

  15. Create a responsive layout.

  16. Test the theme using multiple posts and pages.

Expected Result: A working custom WordPress classic theme with a header, navigation, blog listing, individual post layout and footer.

15. Frequently Asked Questions

Q1. What is Template Hierarchy in WordPress?

Template Hierarchy is the system WordPress uses to select the correct theme template for a requested page.

Q2. What is the purpose of index.php?

In a classic theme, index.php is the main fallback template when a more specific template is unavailable.

Q3. What is the purpose of functions.php?

It contains theme-specific PHP functionality, such as theme setup, menu registration and asset loading.

Q4. What is the WordPress Loop?

The WordPress Loop retrieves and displays posts from the current query.

Q5. What is the difference between single.php and page.php?

single.php generally displays individual blog posts, whereas page.php displays individual static pages.

Q6. What is the difference between front-page.php and home.php?

front-page.php controls the site's front page, while home.php controls the blog posts index.

Q7. What is get_header() in WordPress?

It includes the theme's header template, usually header.php.

Q8. What is wp_head()?

It is a WordPress hook that allows themes and plugins to add required content inside the HTML head section.

Q9. What is wp_footer()?

It is a WordPress hook commonly used to output scripts and other content before the closing body tag.

Q10. Can WordPress themes be developed using PHP?

Yes. Classic WordPress themes use PHP template files, WordPress Template Tags and The Loop.

Q11. What is the difference between a classic theme and a block theme?

Classic themes primarily use PHP templates, while block themes use HTML templates made from WordPress blocks.

Q12. Why should developers use enqueue functions?

Enqueue functions allow WordPress to manage stylesheets and scripts properly, including dependencies and loading order.

16. Conclusion

In this tutorial, we explored how WordPress themes work internally using PHP.

We learned:

  • WordPress theme directory structure.

  • Important theme files.

  • PHP-based theme development.

  • WordPress Template Hierarchy.

  • The WordPress Loop.

  • Template Tags.

  • Conditional Tags.

  • Header and footer integration.

  • CSS and JavaScript enqueue methods.

  • Classic themes versus block themes.

  • Practical custom theme development.

Final Learning Outcome: You can now understand the internal structure of a WordPress classic theme, identify which PHP template controls a webpage and create a basic custom theme using PHP.

←
Post 36 of 40
→