Chapter 5: Theme Development
Chapter 5 of 15
Chapter 5: Theme Development
5.1 Theme Structure
WordPress themes follow a specific structure.
Required Files:
- style.css: Theme stylesheet with header
- index.php: Main template file
- functions.php: Theme functions
5.2 Template Hierarchy
WordPress selects templates based on hierarchy.
// Template hierarchy example
single.php
single-{post_type}.php
single-{post_type}-{slug}.php
singular.php
index.php
5.3 Child Themes
Use child themes to customize parent themes safely.
/*
Theme Name: Child Theme
Template: parent-theme
*/
// Enqueue parent theme styles
function child_theme_enqueue_styles() {
wp_enqueue_style('parent-style', get_template_directory_uri() . '/style.css');
}
add_action('wp_enqueue_scripts', 'child_theme_enqueue_styles');
5.4 Theme Customization API
Use the Customizer API for theme options.
// Add customizer section
add_action('customize_register', 'my_theme_customize_register');
function my_theme_customize_register($wp_customize) {
$wp_customize->add_section('my_section', array(
'title' => 'My Section',
'priority' => 30,
));
$wp_customize->add_setting('my_setting', array(
'default' => 'default_value',
));
$wp_customize->add_control('my_setting', array(
'label' => 'My Setting',
'section' => 'my_section',
'type' => 'text',
));
}
5.5 Theme Development Best Practices
Follow best practices for theme development.
- Use semantic HTML5
- Follow WordPress coding standards
- Make themes responsive
- Optimize for performance
- Ensure accessibility
- Test across browsers