With Tracy Levesque
@LilJimmi • tracy@yikesinc.com • TheTracyL.com
Slides located at: gdi.TheTracyL.com/building-themes
A theme not only determines how a site looks, it can also add functionality to a site. You can build plugin-like functionality right into a theme.
Right now there are over 3,020 free themes in the WordPress Themes Directory.
WordPress currently comes with 3 themes: Twenty Fifteen, Twenty Fourteen and Twenty Thirteen.
Under Appearance > Themes you can see all installed themes. The theme in use is labeled "Active."
NO!
This means do not edit:
If you break something you can just hit undo or remove your file. All parent theme files will remain intact.
Define the general information for your theme.
/* Theme Name: [Your Theme Name] Theme URI: [URL for your theme if you have one] Description: A description for your theme. Author: [You] Author URI: [Your URL] Template: twentythirteen Version: 1 */
Define the general information for your theme.
Enqueue the parent and child theme stylesheets
<?php add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' ); function theme_enqueue_styles() { wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' ); wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array('parent-style') ); }
This is the thumbnail image that represents each theme in Appearance > Themes in the WordPress admin.
Create a 880px by 660px image file, name it “screenshot.png” and place it into the child theme’s folder.
Go to Appearance > Themes in the WP admin. Your child theme is now there!
The 3 files illustrate how a child theme's files affect the parent's files -- they either override and add functionality to its identically named file, or completely replaces it.
Your style.css file will override styles in the parent theme's style.css file with the same selectors.
Example: Changing the size of the header title. The font-size for .site-title is 60px. Use the css selector in your child theme to change it.
.site-title { font-size: 40px; }
First, an introduction to templates...
In the twentythirteen folder is all the theme's the template files. You can create your own versions of these files in your child theme.
Example: Removing WordPress credit from footer.php
Open footer.php in the twentythirteen folder and save a copy into your theme's folder. Alter the contents of .site-info and save the file.
<div class="site-info"> <?php do_action( 'twentythirteen_credits' ); ?> <a href="<?php echo esc_url( __( 'http://wordpress.org/', 'twentythirteen' ) ); ?>" title="<?php esc_attr_e( 'Semantic Personal Publishing Platform', 'twentythirteen' ); ?>"><?php printf( __( 'Proudly powered by %s', 'twentythirteen' ), 'WordPress' ); ?></a> </div?><!-- .site-info -->
Twentythirteen has just one default template, a content area with a right sidebar.
You can make additional templates. Templates you create will appear in the Template drop-down menu on the Page edit screen.
<?php /* Template Name: [Type your template name here] */ ?>
<?php get_header(); ?> <?php get_footer(); ?>
Example: Create a full-width, no sidebar template.
Open page.php in the twentythirteen folder. Rename it page-fullwidth.php and save it into your theme's folder Add the Template Name: to top of the file Remove <?php get_sidebar(); ?> Adjust the css to make .entry-content full width.WordPress dynamically adds classes to the body tag depending what page you are on or template you are using. Use them to create unique styles for pages.
<?php get_search_form(); ?> <?php get_sidebar(); ?> <?php comments_template(); ?>
Check out the WordPress Codex Include Tags Page
Example: Add the search form to the header
Open header.php in the twentythirteen folder and save a copy into your theme's folder. Add <?php get_search_form(); ?> to the header just below the site description<?php the_title(); ?> <?php the_content(); ?> <?php the_permalink(); ?> <?php the_excerpt(); ?> <?php get_the_post_thumbnail(); ?>
Check out the WordPress Codex Function Reference
Example: Add a copyright to the footer
Open footer.php in your theme's folder. Add code to create a copyright lineCopyright © <?php echo date('Y'); ?> <a href="<?php echo home_url( '/' ); ?>"><?php bloginfo( 'name' ); ?></a>
is_front_page() is_home() is_single() is_page() is_category()
Check out the WordPress Codex Conditional Tags Page and this blog post on is_front_page() vs. is_home()
Example: Add credit to the footer that only shows on the home page
Open footer.php Add code to create a credit line<?php if(is_front_page()){ echo "<p>Web design by [your name here]</p>"; } ?>
Example: Add a div to the header that only shows on the home page
header.php
<?php if (is_front_page() ) {?> <div class='header-homediv'> <h1>THIS IS THE HOME PAGE</h1> </div> <?php } ?>
get_template_part is a special include tag that allows you to load any other template file into a template. It lets you to reuse code in multiple templates. Important Dev rule #2 Don't Repeat Yourself.
Check out the WordPress Codex get template part
<?php get_template_part( 'content', 'none' ); ?>
Will load a template file named content-none.php
<?php the_post_thumbnail('medium'); ?>
This will create an img tag for the medium sized version of a post or page's "Featured Image"
Check out the WordPress Codex the post thumbnail
You can bring up the thumbnail, medium, large, original or a custom size of the featured image. If no size is defined it will default to the thumbnail size.
<?php the_post_thumbnail('thumbnail'); ?> <?php the_post_thumbnail('medium'); ?> <?php the_post_thumbnail('large'); ?> <?php the_post_thumbnail('full'); ?> <?php the_post_thumbnail( array(250,100) ); ?>
Check out the WordPress Codex get template directory uri and get stylesheet directory uri
To include images in your theme you can use the code below.
<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/image.png" />VS.
<img src="http://whatever.com/wp-content/mytheme/images/image.png" />
If you name a template file a certain way, it will automatically apply to a certain page.
There's a chart on WordPress.org that shows how the naming conventions work.
The chart looks confusing, but if you break it down it's really pretty simple. We'll look at the template hierarchy for category archives.
If you name template a file a certain way they will affect the display of a certain page. Just follow the chart to find the correct naming convention.
Custom archive and post templates can be named by id or slug
Check out this interactive version of the chart
Example: Make an archive term template
Open category.php in the twentythirteen folder. Rename it category-[slug].php and save it into your theme's folder Modify the templateindex.php is the end of the road. Any page that does not have another template file made for it will use index.php aka the default template for the posts (blog) page.
Child themes are great for modifying existing themes, but you can create your own custom theme with a starter theme.
Starter themes have base WordPress functionality, but very little or no style. Starter theme developers encourage you to hack it and make it your own.
Starter themes can also come with a base framework, like HTML 5 Boilerplate, Twitter Bootstrap or a responsive grid if you enjoy using frameworks. There's no need to reinvent the wheel!