📖 Scenario: You want to create a simple WordPress theme from scratch to understand how themes work. This theme will have a basic structure with a header, footer, and a main content area.
🎯 Goal: Build a minimal WordPress theme with the required files and basic template tags to display a site title and content.
📋 What You'll Learn
Create a style.css file with theme header information
Create an index.php file with basic HTML structure and WordPress loop
Add a functions.php file to enqueue the stylesheet
Add header and footer template parts and include them in index.php
💡 Why This Matters
🌍 Real World
Creating a WordPress theme from scratch helps you understand how themes control site appearance and content display.
💼 Career
WordPress theme development is a common job skill for web developers working with content management systems.
Progress0 / 4 steps
1
Create the theme stylesheet with header
Create a file named style.css with the exact theme header comment including Theme Name: My First Theme and set the body background color to #f0f0f0.
Wordpress
Hint
The theme header comment must start with /* and include Theme Name: My First Theme.
2
Create the main template file with basic HTML and WordPress loop
Create a file named index.php with a basic HTML5 structure. Inside the <body>, add a WordPress loop using have_posts() and the_post() to display the post title with the_title() and content with the_content().
Wordpress
Hint
Use have_posts() and the_post() to loop through posts and display the title and content.
3
Add functions.php to enqueue the stylesheet
Create a file named functions.php and add a function called my_theme_enqueue_styles that uses wp_enqueue_style to load the theme's style.css. Hook this function to wp_enqueue_scripts.
Wordpress
Hint
Use add_action to hook your enqueue function to wp_enqueue_scripts.
4
Add header.php and footer.php and include them in index.php
Create header.php with the opening HTML tags and site title using bloginfo('name'). Create footer.php with closing </body> and </html> tags. Modify index.php to include get_header() at the top and get_footer() at the bottom inside the <body>.
Wordpress
Hint
Use get_header() and get_footer() in index.php to include the header and footer templates.
Practice
(1/5)
1. What is the minimum required file to create a basic WordPress theme from scratch?
easy
A. style.css
B. functions.php
C. header.php
D. footer.php
Solution
Step 1: Understand WordPress theme basics
A WordPress theme must have a style.css file to be recognized by WordPress.
Step 2: Identify the minimum files
While index.php is also needed for templates, style.css is the absolute minimum to register the theme.
Final Answer:
style.css -> Option A
Quick Check:
Minimum theme file = style.css [OK]
Hint: Remember: style.css registers the theme in WordPress [OK]
Common Mistakes:
Thinking functions.php is mandatory for a basic theme
Confusing header.php as required initially
Assuming footer.php is needed to start
2. Which of the following is the correct way to include the header template in a WordPress theme file?
easy
A. include('header.php');
B. load_header();
C. require('header.php');
D. get_header();
Solution
Step 1: Recall WordPress template functions
WordPress provides get_header() to load the header.php template properly.
Step 2: Compare with PHP includes
Using include or require bypasses WordPress hooks and is not recommended.
Final Answer:
get_header(); -> Option D
Quick Check:
Use get_header() to load header template [OK]
Hint: Use get_header() for proper WordPress header loading [OK]
Common Mistakes:
Using PHP include instead of get_header()
Trying to call a non-existent load_header() function
Using require which skips WordPress hooks
3. Given this code snippet in index.php of a WordPress theme:
The code checks if posts exist with have_posts(). If none, it runs the else block.
Step 2: Identify the else block output
The else block outputs <p>No posts found.</p> when no posts exist.
Final Answer:
No posts found. -> Option C
Quick Check:
No posts triggers else output [OK]
Hint: If no posts, the else block runs showing 'No posts found.' [OK]
Common Mistakes:
Assuming no output if no posts
Expecting an error message instead
Thinking titles show even if no posts
4. You created a new theme but WordPress shows a blank page. Which of the following is the most likely cause?
medium
A. Missing style.css file with theme header comment
B. Using get_footer() instead of footer.php
C. Calling get_header() twice in index.php
D. Not adding functions.php file
Solution
Step 1: Check theme registration requirements
WordPress requires style.css with a proper header comment to recognize the theme.
Step 2: Understand blank page cause
If style.css is missing or header comment is absent, WordPress won't load the theme, showing a blank page.
Final Answer:
Missing style.css file with theme header comment -> Option A
Quick Check:
Missing style.css causes blank theme page [OK]
Hint: Always include style.css with header comment to register theme [OK]
Common Mistakes:
Thinking get_footer() usage causes blank page
Assuming duplicate get_header() breaks theme
Believing functions.php is mandatory for theme to load
5. You want to create a WordPress theme from scratch that shows a custom message only on the homepage and lists post titles on other pages. Which code snippet correctly implements this in index.php?
hard
A. <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; else : ?>
<p>Welcome to my custom homepage!</p>
<?php endif; ?>
B. <?php if ( is_front_page() ) : ?>
<p>Welcome to my custom homepage!</p>
<?php else : if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; endif; ?>
C. <?php if ( is_home() ) : ?>
<p>Welcome to my custom homepage!</p>
<?php else : ?>
<h2>No posts to show</h2>
<?php endif; ?>
D. <?php if ( is_front_page() ) : ?>
<p>Welcome to my custom homepage!</p>
<?php endif; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
Solution
Step 1: Use conditional tags correctly
is_front_page() checks if the current page is the homepage to show the custom message.
Step 2: Show posts only on other pages
Else block runs the loop to list post titles on non-homepage pages.
Step 3: Verify code structure
<?php if ( is_front_page() ) : ?>
<p>Welcome to my custom homepage!</p>
<?php else : if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; endif; ?> nests the else with have_posts() loop correctly, showing message only on homepage.
Final Answer:
The nested if-else code snippet -> Option B
Quick Check:
Conditional homepage message + posts elsewhere = <?php if ( is_front_page() ) : ?>
<p>Welcome to my custom homepage!</p>
<?php else : if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; endif; ?> [OK]
Hint: Use is_front_page() for homepage, else loop for posts [OK]
Common Mistakes:
Putting posts loop inside homepage condition
Showing message only when no posts exist
Not separating homepage and other pages logic
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.