http://zooninidev.com/child-theming/
Use ←left & right→ arrow keys to navigate
penscratch-child
Child's folder name could theoretically be anything
No spaces or special characters
style.css
This file is required in a child theme
style.css
Add to top of file:
/*
Theme Name: Penscratch Child
Description: An original take on Penscratch
Author: Your Name
Template: penscratch
Version: 1.0.0
*/
Only required lines: Theme Name & Template
Template Name must match parent folder name
(case sensitive!)
Find Penscratch Child
Hover and click Theme Details
Some details from style.css are pulled in here
You can add a screenshot later once your child theme is finished
What's missing? Why?
We could write our child themes's CSS from scratch, but in our case we want to build on Penscratch's existing CSS.
functions.php
functions.php
Add opening PHP tag at the top with nothing before it:
Then let's enqueue Penscratch's CSS file:
add_action( 'wp_enqueue_scripts', 'my_child_enqueue_styles' );
function my_child_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}
Refresh, how does it look now?
Familiar? It looks exactly like the parent, right?
Use browser inspector
style.css
/* Change colour and style of tagline */
.site-description {
color: #b2502c;
text-transform: uppercase;
}
Use hex colour from earlier, or a colour picker: example 1, example 2
style.css
/* Add border around page content */
.site {
border: 8px solid #eec9bb;
border-radius: 10px;
}
If you ONLY wanted to add CSS to tweak a theme, do you need a child theme?
NO!
A custom CSS editor is included with the Customizer.
Add our own text with a link
footer.php
footer.php
Open child theme's file in editor
Look at lines 14-18
footer.php
Remove everything between opening and closing site-info div (ln. 15-17)
Replace with your own HTML
This site lovingly created by Kathryn Presner in June 2016
header.php
Let's move the custom header above the menu
header.php
header.php
Open child theme's file in editor
Find lines 38-42
header.php
Move those header lines just above this nav element on line 31
header.php
content.php
get_template_part( 'content', get_post_format() );content.php
Look at ln. 32-38
content.php
Change:
if ( is_search() ) : // Only display Excerpts for Search
to:
if ( is_search() || is_home() ) : // Only display Excerpts for Search or Blog
|| means "or" in PHP
is_home() is a WordPress conditional tag meaning "is the blog page"
Other fun things to do with templates in a child theme:
To name a few!
Copy the widget initialization function into our child theme as is? You don't need to actually do this :)
function penscratch_widgets_init() {
register_sidebar( array(
'name' => __( 'Sidebar', 'penscratch' ),
'id' => 'sidebar-1',
'description' => '',
'before_widget' => '',
'before_title' => '',
'after_title' => '
',
) );
}
add_action( 'widgets_init', 'penscratch_widgets_init' );
Why did this happen?
If a function is pluggable you
can override it by copying it into child.
How to identify a pluggable function:
if ( ! function_exists( 'penscratch_site_logo' ) ) :
function penscratch_site_logo() {
add_image_size( 'penscratch-site-logo', '300', '300' );
}
endif;
add_action( 'after_setup_theme', 'penscratch_site_logo' );
Example function only, not in the theme
You could remove the if/endif condition and copy the function into the child theme with changes:
function penscratch_site_logo() {
add_image_size( 'penscratch-site-logo', '500', '500' );
}
add_action( 'after_setup_theme', 'penscratch_site_logo' );
Example function only, not actually in the theme
Open the parent theme's functions.php file.
How many pluggable functions are there?
penscratch_content_width()penscratch_setup()Note: functions (pluggable or not) can also be in other files besides functions.php
Setup functions can be long.
What if we just want to override a small part of it?
Action hooks and filter hooks can modify an existing function, in core or theme
From the Codex:
Actions allow you to add or remove code from existing Actions.
Filters allow you to replace specific data (such as a variable) found within an existing Action.
[...] to "Continue Reading"
[...] to "Continue Reading"
function penscratch_child_excerpt_length( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'penscratch_child_excerpt_length', 999 );
function penscratch_child_excerpt_more($more) {
global $post;
return '... Continue Reading';
}
add_filter('excerpt_more', 'penscratch_child_excerpt_more');
Codex references: excerpt length, read more
Look at lines 80-82
add_theme_support( 'post-formats', array(
'aside', 'image', 'video', 'quote', 'link'
) );
functions.php
What if we want to add support for gallery post format?
Make a function for our child theme, adding gallery to the array.
function penscratch_child_formats() {
add_theme_support( 'post-formats', array(
'aside', 'image', 'video', 'quote', 'link', 'gallery'
) );
}
add_action( 'after_setup_theme', 'penscratch_child_formats', 11 );
The number 11 at the end is the priority, which loads this function after the main setup to override it. (Default is 10.)
Open one of your posts and choose the Gallery format for it.
Look at functions.php in parent for ideas
To name a few!
Experiment with more tweaks on your own!