67
THEME DEVELOPMENT an introduction to the basics of theming with WordPress

Intro to WordPress theme development

Embed Size (px)

DESCRIPTION

An introduction to WordPress theme development by Thad Allender from GraphPaperPress.com. Slides from WordPress December 2010 Meetup at Fathom Creative in Washington, D.C.

Citation preview

Page 1: Intro to WordPress theme development

THEME DEVELOPMENTan introduction to the basics of theming with WordPress

Page 2: Intro to WordPress theme development

About me

• Name: Thad Allender

• Web: ThadAllender.com & GraphPaperPress.com

• Twitter: @thadallender

• Email: [email protected]

Page 3: Intro to WordPress theme development

Poll

•Blogger?

•Designer?

•Developer?

Page 4: Intro to WordPress theme development

Anatomy of a WordPress theme

• WordPress Themes are files that work together to create the design and functionality of a WordPress site

• http://codex.wordpress.org/Theme_Development

Page 5: Intro to WordPress theme development

Anatomy of a WordPress theme

• Themes live in /wp-content/themes/

• Themes include stylesheets, template & functions files, images, javascripts

Page 6: Intro to WordPress theme development

Anatomy of a WordPress theme• style.css – The main stylesheet. This must be included with your theme.

• index.php – The main template. If your theme provides its own templates, index.php must be present.

• comments.php – The comments template. If not present, wp-comments.php is used.

• single.php – The single post template. Used when a single post is queried. For this and all other query templates, index.php is used if the query template is not present.

• page.php – The page template. Used when a page is queried.

• category.php – The category template. Used when a category is queried.

• author.php – The author template. Used when an author is queried.

• date.php – The date/time template. Used when a date or time is queried. Year, month, day, hour, minute, second.

• archive.php – The archive template. Used when a category, author, or date is queried. Note that this template will be overridden by category.php, author.php, and date.php for their respective query types.

• search.php – The search template. Used when a search is performed.

• 404.php – The 404 Not Found template. Used when WordPress cannot find a post that matches the query.

Page 7: Intro to WordPress theme development

style.css/*

Theme Name: Twenty Ten

Theme URI: http://wordpress.org/

Description: The theme description.

Author: the WordPress team

Version: 1.3-alpha

Tags: black, two-columns, fixed-width, custom-header

*/

Page 8: Intro to WordPress theme development

index.php<?php get_header(); ?>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

! <h1><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>

! <?php the_content(); ?>

<?php endwhile; else: ?>

! <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>

<?php endif; ?>

<?php get_footer(); ?>

Page 9: Intro to WordPress theme development

Page Structure

• Made up of three basic building blocks: a header, the content, and a footer. Sidebars add additional functionality.

Page 10: Intro to WordPress theme development

header.php

• Called with: get_header()

• Includes everything that comes before and including the <body>, meta info, scripts, styles, wp_head, site name, navigation.

Page 11: Intro to WordPress theme development

index.php

• Typically displays content from the loop (title, content, etc.)

• Calls get_header(), get_sidebar(), get_footer

Page 12: Intro to WordPress theme development

footer.php

• Called with: get_footer()

• Can include credits, copyright info, wp_footer hook.

Page 13: Intro to WordPress theme development

sidebar.php

• Called with get_sidebar()

• Adds contextual site info. Typically includes widgets.

Page 14: Intro to WordPress theme development

Template Hierarchy

• The order in which WordPress template files are chosen

• http://codex.wordpress.org/Template_Hierarchy

Page 15: Intro to WordPress theme development

Template Hierarchy

Page 16: Intro to WordPress theme development

Home Page Display

1.home.php

2.index.php

Page 17: Intro to WordPress theme development

Single Post Display

1.single-{post_type}.php - If the post_type were videos, WordPress would look for single-videos.php.

2.single.php

3.index.php

Page 18: Intro to WordPress theme development

Page Display1.custom template - Where custom template is the Page Template

assigned to the Page.

2.page-{slug}.php - If the page slug is recent-news, WordPress will look to use page-recent-news.php

3.page-{id}.php - If the page ID is 6, WordPress will look to use page-6.php

4.page.php

5.index.php

Page 19: Intro to WordPress theme development

Category Display

1.category-{slug}.php - If the category's slug were news, WordPress would look for category-news.php

2.category-{id}.php - If the category's ID were 6, WordPress would look for category-6.php

3.category.php

4.archive.php

5.index.php

Page 20: Intro to WordPress theme development

Tag Display

1.tag-{slug}.php - If the tag's slug were sometag, WordPress would look for tag-sometag.php

2.tag-{id}.php - If the tag's ID were 6, WordPress would look for tag-6.php

3.tag.php

4.archive.php

5.index.php

Page 21: Intro to WordPress theme development

Custom Post Type Display

1.'has_archive' => true

2.archive-{$post_type}.php

3.archive.php

4.index.php

Page 22: Intro to WordPress theme development

Author Display

1.author-{nicename}.php - If the author's nice name were rami, WordPress would look for author-rami.php.

2.author-{id}.php - If the author's ID were 6, WordPress would look for author-6.php.

3.author.php

4.archive.php

5.index.php

Page 23: Intro to WordPress theme development

Date Display

1.date.php

2.archive.php

3.index.php

Page 24: Intro to WordPress theme development

Search Display

1.search.php

2.index.php

Page 25: Intro to WordPress theme development

404 (Not Found) Display

1.404.php

2.index.php

Page 26: Intro to WordPress theme development

Attachment Display

1.MIME_type.php - it can be any MIME type (image.php, video.php, audio.php, application.php or any other).

2.attachment.php

3.single.php

4.index.php

Page 27: Intro to WordPress theme development

Development Environment

Page 29: Intro to WordPress theme development

Version Control

• Simplifies collaborative development, comparing, releasing

• SVN - http://subversion.tigris.org/

• GIT - http://git-scm.com/

Page 30: Intro to WordPress theme development

The Loop

• Displays each of your posts

• http://codex.wordpress.org/The_Loop

Page 31: Intro to WordPress theme development

The Loop

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

....add template tags here....

<?php endwhile; else: ?>

<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>

<?php endif; ?>

Page 32: Intro to WordPress theme development

Query Posts

• query_posts(‘cat=1&showposts=5’)

• http://codex.wordpress.org/Function_Reference/query_posts

Page 33: Intro to WordPress theme development

query_posts()• Use it to control which posts show up in The Loop.

• Display only a single post on your homepage (a single Page can be done via Settings -> Reading).

• Show all posts from a particular time period.

• Show the latest post (only) on the front page.

• Change how posts are ordered.

• Show posts from only one category.

• Exclude one or more categories.

Page 34: Intro to WordPress theme development

query_posts()<?php

query_posts('posts_per_page=5');

if ( have_posts() ) : while ( have_posts() ) : the_post();

..

endwhile; else:

..

endif;

wp_reset_query();

?>

Page 35: Intro to WordPress theme development

query_posts()

The query_posts function is intended to be used to modify the main page Loop only. It is not intended as a means to create secondary Loops on the page. If you want to create separate Loops outside of the main one, you should use get_posts() instead.

Page 36: Intro to WordPress theme development

Get Posts

• get_posts(‘cat=1&numberposts=3’)

• http://codex.wordpress.org/Template_Tags/get_posts

Page 37: Intro to WordPress theme development

get_posts()<ul>

<?php

global $post;

$myposts = get_posts('posts_per_page=5&offset=1&category=1');

foreach($myposts as $post) :

setup_postdata($post);

?>

<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

<?php endforeach; ?>

</ul>

Page 38: Intro to WordPress theme development

Template Tags

• Used to display dynamic information about your site

• http://codex.wordpress.org/Template_Tags

Page 39: Intro to WordPress theme development

Include Tags

• get_header()

• http://codex.wordpress.org/Include_Tags

Template include tags are used within one template file (for example index.php) to execute the HTML and PHP found in another template file (for example header.php).

Page 40: Intro to WordPress theme development

Include Tags• get_header() - includes header.php or header-{name}.php

• get_footer() - includes footer.php or footer-{name}.php

• get_sidebar() - includes sidebar.php or sidebar-{name}.php

• get_template_part() - includes {slug}.php or {slug}-{name}.php

• get_search_form() - includes searchform.php

• comments_template() - includes comments.php or wp-includes/theme-compat/comments.php

Page 41: Intro to WordPress theme development

Body Class

• body_class()

• http://codex.wordpress.org/Function_Reference/body_class

Page 42: Intro to WordPress theme development

body_class()

• <body <?php body_class(); ?>

• <body class="archive category category-daily-photo">

Helps theme authors style more effectively with CSS by applying different classes to the

body element

Page 43: Intro to WordPress theme development

Post Class

• post_class()

• http://codex.wordpress.org/Function_Reference/post_class

Page 44: Intro to WordPress theme development

post_class()

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

<article id="post-1167" class="post-1167 post type-post hentry category-daily-photo tag-dc tag-georgetown tag-washington">

Page 46: Intro to WordPress theme development

get_template_part()• get_template_part( 'loop', 'index' );

• Looks for a file named ‘loop-index.php’ in the current theme

• If not found, looks for ‘loop.php’

• If this is a child theme, and neither of those templates exist, looks in the parent theme.

• Each of the major templates (‘archive.php’, ‘author.php’, ‘category.php’, etc) makes a call similar to this, looking for a loop template specific to the appropriate view.

Page 47: Intro to WordPress theme development

Post Formats

• has_post_format()

• http://codex.wordpress.org/Post_Formats

Page 48: Intro to WordPress theme development

Post Formats

Page 49: Intro to WordPress theme development

while ( the_loop() ):

if ( has_post_format( 'gallery' ) ) :

// big block of HTML to format a gallery post

elseif ( has_post_format( 'video' ) ) :

// big block of similar HTML to format a video post

elseif ( has_post_format( 'image' ) ) :

// big block of similar HTML to format an image post

elseif ( has_post_format( 'aside' ) ) :

// big block of similar HTML to format an aside

else :

// big block of similar HTML to format other posts

endif;

endwhile;

Page 50: Intro to WordPress theme development

get_template_part() and get_post_format()

while ( the_loop() ):

get_template_part( 'format', get_post_format() );

endwhile;

Page 51: Intro to WordPress theme development

get_template_part() and get_post_format()

If the current post has post format ‘Link’, then we look for a file named ‘format-link.php’. If the current post has format ‘Aside’, we look for ‘format-aside.php’, if it’s a Quote, ‘format-quote.php’, regular posts look for ‘format-standard.php’, and failing all else, we use plain old ‘format.php’.

Page 53: Intro to WordPress theme development

wp_enqueue_script()<?php

function my_init_method() {

wp_deregister_script( 'jquery' );

wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js');

wp_enqueue_script( 'jquery' );

}

add_action('init', 'my_init_method');

?>

Page 55: Intro to WordPress theme development

Conditional Tags

• http://codex.wordpress.org/Conditional_Tags

Used in Template files to change what content is displayed and how that content is displayed on a particular page depending on what conditions that page matches

Page 56: Intro to WordPress theme development

Conditional Tags

if (is_home) {

echo ‘This is home!’;

} else {

echo ‘No home!’;

}

Page 57: Intro to WordPress theme development

Confused? Comment your code

• <!-- END #container -->

• // PHP comment goes here

Page 58: Intro to WordPress theme development

Child Themes

• Inherits the functionality of another theme, called the parent theme, and allows you to modify, or add to, the functionality of that parent theme

• http://codex.wordpress.org/Child_Themes

Page 59: Intro to WordPress theme development

Child themes: style.css/*

Theme Name: TwentyTen Child

Template: twentyten

Version: 1.0

Author: Thad Allender

*/

@import url("../twentyten/style.css");

Page 60: Intro to WordPress theme development

Child themes: functions.php

The functions.php of a child theme does not override its counterpart from the parent. Instead, it is loaded in addition to the parent’s functions.php. (Specifically, it is loaded right before the parent’s file.)

Page 61: Intro to WordPress theme development

Child Themes: Including files

TEMPLATEPATH - returns the path to the template files of a theme

STYLESHEETPATH - returns the path to the template files of the child theme

require_once( STYLESHEETPATH . '/path/to/file/in/child/theme.php' );

Page 62: Intro to WordPress theme development

Child themes: File Overrides

•All other template files override their namesakes from the parent.

•Example: twentyten-child/single.php overrides twentyten/single.php

•This holds true to all templates in the WordPress hierarchy and new templates introduced using get_template_part()

Page 63: Intro to WordPress theme development

Child Themes: ApproachesBecause a child theme’s functions.php is loaded first, you can make the user functions of your theme pluggable—that is, replaceable by a child theme—by declaring them conditionally in the parent.

if (!function_exists('my_index_loop')) {

function my_index_loop() {

// Do something.

}

}

In that way, a child theme can replace a PHP function of the parent by simply declaring it again.

Page 64: Intro to WordPress theme development

Child Themes: Hooks

Enables you to add, remove or change code in supporting parent themes

http://themeshaper.com/action-hooks-wordpress-child-themes/

Page 65: Intro to WordPress theme development

Theme Testing

Page 67: Intro to WordPress theme development

Questions?