🚀 Homework: Expand Your WordPress Theme
Apply template hierarchy concepts to build a professional theme structure
Estimated Time: 3-4 hours | Due: Before Session 3
Assignment Overview
Building on your minimal theme from Session 1, you'll now expand it with proper template hierarchy, modular template parts, and professional structure. This hands-on assignment will solidify your understanding of how WordPress selects templates and how to organize theme files effectively.
Core Assignments
Create Template Hierarchy Structure
Build out the main template files for different content types:
- Create
single.phpfor blog posts - Create
page.phpfor static pages - Create
archive.phpfor archive pages - Create
search.phpfor search results - Create
404.phpfor error pages
Implement Template Parts
Modularize your theme with reusable template parts:
- Create a
template-parts/folder - Build
content.phpfor default post content - Build
content-page.phpfor page content - Build
content-none.phpfor no results - Use
get_template_part()in main templates
Add Category Templates
Create specialized category templates:
- Create
category.phpfor all categories - Create a specific category template (e.g.,
category-news.php) - Add category description display
- Customize the layout for categories
Create Custom Page Template
Build a custom page template with unique layout:
- Create
page-templates/full-width.php - Add proper template header comment
- Remove sidebar for full-width layout
- Test template selection in page editor
- Style appropriately with CSS
Implement Header/Footer Variations
Create conditional header/footer loading:
- Create
header-home.phpfor homepage - Create
footer-simple.phpfor specific pages - Use
get_header('home')conditionally - Use
get_footer('simple')conditionally - Add visual differences to each variation
Pass Arguments to Template Parts
Use WordPress 5.5+ argument passing:
- Pass custom classes to template parts
- Pass display options (show/hide elements)
- Handle arguments with defaults
- Create backwards-compatible fallback
Expected File Structure
After completing the assignments, your theme structure should look like this:
my-theme/ ├── style.css ├── index.php ├── functions.php ├── header.php ├── header-home.php ← New ├── footer.php ├── footer-simple.php ← New ├── sidebar.php ├── single.php ← New ├── page.php ← New ├── archive.php ← New ├── category.php ← New ├── category-news.php ← New (example) ├── search.php ← New ├── 404.php ← New ├── page-templates/ ← New folder │ └── full-width.php ├── template-parts/ ← New folder │ ├── content.php │ ├── content-page.php │ ├── content-none.php │ └── content-search.php ├── assets/ │ ├── css/ │ ├── js/ │ └── images/ └── screenshot.png
Submission Checklist
🌟 Bonus Challenges
Want to go above and beyond? Try these optional challenges:
- Post Format Support: Add support for different post formats (aside, gallery, video) with unique templates
- Author Templates: Create author.php and author-{id}.php for author archives
- Date Archives: Implement date.php for date-based archives
- Attachment Template: Create attachment.php for media files
- Debug Helper: Build a template hierarchy debugger that shows which template is being used
- Template Part Variations: Create content-{post-format}.php variations
- Ajax Loading: Use get_template_part() in an AJAX handler for dynamic content
Testing Your Theme
How to Test Template Hierarchy
- Homepage: Visit your site root to test front-page.php or home.php
- Single Post: Click any blog post to test single.php
- Page: Visit any static page to test page.php
- Category: Click a category link to test category.php
- Search: Use the search form to test search.php
- 404: Visit a non-existent URL to test 404.php
- Custom Template: Create/edit a page and select your custom template
Debug Tips
// Add to functions.php for debugging
add_action( 'wp_head', function() {
if ( current_user_can( 'manage_options' ) ) {
global $template;
echo '<!-- Template: ' . basename( $template ) . ' -->';
}
});
Common Issues & Solutions
Helpful Resources
📅 Submission Guidelines
Due: Complete before Session 3 - The Loop & Content Display
Zip your entire theme folder and be prepared to discuss your implementation choices in the next session.
We'll review common patterns and best practices based on your submissions!
Pro Tips for Success
Best Practices to Follow
- Start Simple: Get basic templates working before adding complexity
- Test Often: Check each template as you create it
- Use Comments: Document what each template is for
- Keep DRY: Don't Repeat Yourself - use template parts for repeated code
- Follow Naming: Use WordPress naming conventions exactly
- Version Control: Commit your changes frequently with Git
- Browser DevTools: Use inspector to verify which template loads
- Create Content: Add sample posts/pages/categories for testing