https://www.codeinwp.com/blog/wordpress-cheat-sheets-web-development-design/
Languages:
English •
日本語
(Add your language)
WordPress Optimization/Cheat Sheet
This article is part of a series on WordPress Optimization
This is a quick list of the key areas in which WordPress can be optimized. The biggest gain for the least effort comes from caching.
- Server Optimization
- DNS onto a separate server
- Web Server optimization
- PHP acceleration / optimization
- MySQL tweaking (query cache, etc)
- WordPress Performance
- Remove unused plugins
- Optimize plugins
- Optimize themes, hardcode static vars, etc
- Offloading
- Offload static files to separate server
- Optimized web servers like publicfile, lighttpd, etc
- Offload feed traffic
- Caching
- WordPress caching (application level)
- Browser caching (client side)
- Web server caching (server side)
- Adding Database Servers
BIG BREAKDOWN – MORE USER FRIENDLY
There sure is a lot you need to remember when working with WordPress theme files.
From the names of basic template files to functions and how the WordPress Loop works, it’s next to impossible to remember every PHP tag or even how to define a new theme.
About 550 students enrolled in our Academy course, WordPress Development for Beginners, are in their final week, all tackling the final assignment – building a WordPress theme from scratch.
So to help you out, I thought I’d put together this handy cheat sheet, which includes all the files and functions you need to know (but not necessarily remember!) when working with themes. This is definitely one to bookmark and save for future reference!
Good luck with your final assignment, and if you’re not already enrolled in WordPress Development for Intermediate Users, get in quick before enrollments close soon!
Theme Files
These are the basic files that every theme should include:
style.css
– This is your theme’s stylesheet file.index.php
– This is the main body template for your theme. Its job is to bring together all the information in the other theme files using template tags.header.php
– This file contains the header information that appears with the<head>
section of your site, stuff like metadata and the link to your stylesheet.sidebar.php
– Everything in you sidebar goes in this file, like widgets, categories, additional menus, search form, etc.footer.php
– This file contains your footer information, such as copyright details, widgets, and social icons.single.php
– This file displays just one post.page.php
– When you create a page on your site, this is the template responsible.comments.php
– This file is responsible for displaying comments.404.php
– When visitors try to visit a page on your site that doesn’t exist, this file will general an error page.functions.php
– This file is where you can place special functions. We always recommend creating a child theme rather than edit this file directly.archive.php
– Display an archive with this file so visitors to your site can go way back when and read your Hello World! post.search.php
– Help your visitors search your site with this page.searchform.php
– Display a search form for your visitors with this template file.
Defining a New Theme
Your stylesheet doesn’t just contain styling information for your theme – it also holds details about your theme that are displayed in the Appearance > Themes section of your WordPress admin.
The following is an example of the first few lines of the stylesheet for the default Twenty Sixteen theme:
/* | |
Theme Name: Twenty Sixteen | |
Theme URI: https://wordpress.org/themes/twentysixteen/ | |
Author: the WordPress team | |
Author URI: https://wordpress.org/ | |
Description: Twenty Sixteen is a modernized take on an ever-popular WordPress layout — the horizontal masthead with an optional right sidebar that works perfectly for blogs and websites. It has custom color options with beautiful default color schemes, a harmonious fluid grid using a mobile-first approach, and impeccable polish in every detail. Twenty Sixteen will make your WordPress look beautiful everywhere. | |
Version: 1.2 | |
License: GNU General Public License v2 or later | |
License URI: http://www.gnu.org/licenses/gpl-2.0.html | |
Tags: black, blue, gray, red, white, yellow, dark, light, one-column, two-columns, right-sidebar, fixed-layout, responsive-layout, accessibility-ready, custom-background, custom-colors, custom-header, custom-menu, editor-style, featured-images, flexible-header, microformats, post-formats, rtl-language-support, sticky-post, threaded-comments, translation-ready | |
Text Domain: twentysixteen | |
This theme, like WordPress, is licensed under the GPL. | |
Use it to make something cool, have fun, and share what you’ve learned with others. | |
*/ |
This information goes at the top of your stylesheet.css file.
Template Include Tags
Template include tags are used within one template file (for example index.php) to include (or call) the HTML and PHP found in another template file (for example header.php). While PHP has its own built-in include() statement to do this, these WordPress-specific tags make life much easier:
<?php get_header(); ?>
– Includes the header.php file<?php get_sidebar(); ?>
– Includes the sidebar.php file<?php get_footer(); ?>
– Includes the footer.php file<?php comments_template(); ?>
– Includes your comments
Template Header/Bloginfo Tags
These are functions you’ll find in your theme’s header.php file, though you’ll also find them in other theme files:
<?php bloginfo('name'); ?>
– The title of your site, or blog name<?php bloginfo('url'); ?>
– Your site’s URL<?php bloginfo('stylesheet_url'); ?>
– Link to your themes’s stylesheet file<?php bloginfo('template_url'); ?>
– Location of your site’s theme file<?php bloginfo('description'); ?>
– Displays the tagline of your blog as set in Settings > General.<?php bloginfo('atom_url'); ?>
– Link to your site’s atom URL<?php bloginfo('rss2_url'); ?>
– RSS feed URL for your site<?php bloginfo('pingback_url'); ?>
– Pingback URL for your site<?php bloginfo('version'); ?>
– WordPress version number<?php bloginfo('html_type'); ?>
– The HTML version your site is using<?php site_url(); ?>
– The root URL for your site<?php get_stylesheet_directory(); ?>
– Location of your stylesheet folder<?php wp_title(); ?>
– Title of a specific page
Template Tags
These tags can be used across all of your template files, such as index.php or page.php, making it easy to display specific information anywhere you want on your site:
<?php the_content(); ?>
– Displays the content of a post
<?php the_excerpt(); ?>
– Displays the excerpt used in posts
<?php the_title(); ?>
– Title of the specific post
<?php the_permalink() ?>
– Link of a specific post
<?php the_category(', ') ?>
– Category of a specific post
<?php the_author(); ?>
– Author of a specific post
<?php the_ID(); ?>
– ID of a specific post
<?php edit_post_link(); ?>
– Edit link for a post
<?php next_post_link(' %link ') ?>
– URL of the next page
<?php previous_post_link('%link') ?>
– URL of the previous page
<?php get_links_list(); ?>
– Lists all links in blogroll
<?php wp_list_pages(); ?>
– Lists all pages
<?php wp_get_archives() ?>
– List archive for the site
<?php wp_list_cats(); ?>
– Lists all categories
<?php get_calendar(); ?>
– Displays the built-in calendar
<?php wp_register(); ?>
– Displays register link
<?php wp_loginout(); ?>
– Displays login/logout link only to registered users
This is by no means a definitive list of all the template tags available. For the full list, check out the Template Tags entry in the WordPress Codex.
The Loop
The Loop is the default mechanism in WordPress for displaying all of your posts. Exactly how many posts are retrieved is determined by the number of posts you’ve chosen to display in the “Reading” settings in your WordPress dashboard.
Within the Loop, WordPress loops through each post retrieved for the current page one at a time and formats it according to your theme’s instructions.
You can use the Loop to do a lot of useful stuff, like:
- Display post titles and excerpts on your homepage;
- Display the content and comments on a single post;
- Display the content on an individual page using template tags; and
- Display data from custom post types and custom fields.
<?php | |
if ( have_posts() ) : | |
while ( have_posts() ) : | |
the_post(); | |
// | |
// Post Content here | |
// | |
endwhile; // end while | |
endif; // end if | |
?> |
The Loop can display lots of different element for each post. Some of the most common template tags used in themes (according to the WordPress Theme Handbook) are:
next_post_link()
– A link to the post published chronologically after the current postprevious_post_link()
– A link to the post published chronologically before the current postthe_category()
– The category or categories associated with the post or page being viewedthe_author()
– The author of the post or pagethe_content()
– The main content for a post or pagethe_excerpt()
– The first 55 words of a post’s main content followed by an ellipsis (…) or read more link that goes to the full post. You may also use the “Excerpt” field of a post to customize the length of a particular excerpt.the_ID()
– The ID for the post or pagethe_meta()
– The custom fields associated with the post or pagethe_shortlink()
– A link to the page or post using the URL of the site and the ID of the post or pagethe_tags()
– The tag or tags associated with the postthe_title()
– The title of the post or pagethe_time()
– The time or date for the post or page. This can be customized using standard php date function formatting.
You can also use conditional tags, such as:
is_home()
– Returns true if the current page is the homepageis_admin()
– Returns true if an administrator is logged in and visiting the siteis_single()
– Returns true if the page is currently displaying a single postis_page()
– Returns true if the page is currently displaying a single pageis_page_template()
– Can be used to determine if a page is using a specific template, for example:is_page_template('about-page.php')
is_category()
– Returns true if page or post has the specified category, for exampleis_category('news')
is_tag()
– Returns true if a page or post has the specified tagis_author()
– Returns true if a specific author is logged in and visiting the siteis_search()
– Returns true if the current page is a search results pageis_404()
– Returns true if the current page does not existhas_excerpt()
– Returns true if the post or page has an excerpt
And There’s Even More to Know!
I’ve really only scratched the surface with this list. Overwhelming, I know!
While this article offers a handy list to help you with building themes, here are some other cheat sheets worth checking out:
Template Tags – WordPress Codex: It would be remiss of me to leave the Codex off this list since it offers the most comprehensive guide to all of the tags and functions available.
WordPress Help Sheet – Quickly Code: Download this wallpaper and change your desktop background. You’ll never forget a tag ever again!
WordPress Visual Cheat Sheet – Artist Relations: This is one of my favorites because it is so freaking detailed. Definitely worth bookmarking.
WordPress Cheat Sheet – tuts+: This compact guide provides a concise overview of WordPress theme files, the Loop and template tags.
WordPress Mega Cheat Sheet – Make a Website Hub: I’m also a fan of this cheat sheet, which also include keyboard shortcuts.
Template Heirarchy
Template Hierarchy
Topics
As discussed, template files are modular, reusable files, used to generate the web pages on your WordPress site. Some template files (such as the header and footer template) are used on all of your site’s pages, while others are used only under specific conditions.
This article explains how WordPress determines which template file(s) to use on individual pages. If you want to customize an existing WordPress theme it will help you decide which template file needs to be edited.
The Template File Hierarchy
Overview
WordPress uses the query string to decide which template or set of templates should be used to display the page. The query string is information that is contained in the link to each part of your website. It comes after the initial question mark and may contain a number of parameters separated by ampersands.
Put simply, WordPress searches down through the template hierarchy until it finds a matching template file. To determine which template file to use, WordPress:
- Matches every query string to a query type to decide which page is being requested (for example, a search page, a category page, etc);
- Selects the template in the order determined by the template hierarchy;
- Looks for template files with specific names in the current theme’s directory and uses the first matching template file as specified by the hierarchy.
With the exception of the basic index.php
template file, you can choose whether you want to implement a particular template file or not.
If WordPress cannot find a template file with a matching name, it will skip to the next file in the hierarchy. If WordPress cannot find any matching template file, the theme’s index.php
file will be used.
If your blog is at http://example.com/blog/
and a visitor clicks on a link to a category page such as http://example.com/blog/category/your-cat/
, WordPress looks for a template file in the current theme’s directory that matches the category’s ID to generate the correct page. More specifically, WordPress follows this procedure:
- Looks for a template file in the current theme’s directory that matches the category’s slug. If the category slug is “unicorns,” then WordPress looks for a template file named
category-unicorns.php
. - If
category-unicorns.php
is missing and the category’s ID is 4, WordPress looks for a template file namedcategory-4.php
. - If
category-4.php
is missing, WordPress will look for a generic category template file,category.php
. - If
category.php
does not exist, WordPress will look for a generic archive template,archive.php
. - If
archive.php
is also missing, WordPress will fall back to the main theme template file,index.php
.
The following diagram shows which template files are called to generate a WordPress page based on the WordPress template hierarchy.
While the template hierarchy is easier to understand as a diagram, the following sections describe the order in which template files are called by WordPress for a number of query types.
By default, WordPress sets your site’s home page to display your latest blog posts. This page is called the blog posts index. You can also set your blog posts to display on a separate static page. The template file home.php
is used to render the blog posts index, whether it is being used as the front page or on separate static page. If home.php
does not exist, WordPress will use index.php
.
home.php
index.php
Note: If front-page.php
exists, it will override the home.php
template.
The front-page.php
template file is used to render your site’s front page, whether the front page displays the blog posts index (mentioned above) or a static page. The front page template takes precedence over the blog posts index (home.php
) template. If the front-page.php
file does not exist, WordPress will either use the home.php
or page.php
files depending on the setup in Settings → Reading. If neither of those files exist, it will use the index.php
file.
front-page.php
– Used for both “your latest posts” or “a static page” as set in the front page displays section of Settings → Reading.home.php
– If WordPress cannot findfront-page.php
and “your latest posts” is set in the front page displays section, it will look forhome.php
. Additionally, WordPress will look for this file when the posts page is set in the front page displays section.page.php
– When “front page” is set in the front page displays section.index.php
– When “your latest posts” is set in the front page displays section buthome.php
does not exist or when front page is set butpage.php
does not exist.
As you can see, there are a lot of rules to what path WordPress takes. Using the chart above is the best way to determine what WordPress will display.
The privacy-policy.php
template file is used to render your site’s Privacy Policy page. The Privacy Policy page template takes precedence over the static page (page.php
) template. If the privacy-policy.php
file does not exist, WordPress will either use the page.php
or singular.php
files depending on the available templates. If neither of those files exist, it will use the index.php
file.
-
privacy-policy.php
– Used for the Privacy Policy page set in the Change your Privacy Policy page section of Settings → Privacy. custom template file
– The page template assigned to the page. Seeget_page_templates()
.page-{slug}.php
– If the page slug isprivacy
, WordPress will look to usepage-privacy.php
.page-{id}.php
– If the page ID is 6, WordPress will look to usepage-6.php
.page.php
singular.php
index.php
The single post template file is used to render a single post. WordPress uses the following path:
single-{post-type}-{slug}.php
– (Since 4.4) First, WordPress looks for a template for the specific post. For example, if post type isproduct
and the post slug isdmc-12
, WordPress would look forsingle-product-dmc-12.php
.single-{post-type}.php
– If the post type isproduct
, WordPress would look forsingle-product.php
.single.php
– WordPress then falls back tosingle.php
.singular.php
– Then it falls back tosingular.php
.index.php
– Finally, as mentioned above, WordPress ultimately falls back toindex.php
.
The template file used to render a static page (page
post-type). Note that unlike other post-types, page
is special to WordPress and uses the following path:
custom template file
– The page template assigned to the page. Seeget_page_templates()
.page-{slug}.php
– If the page slug isrecent-news
, WordPress will look to usepage-recent-news.php
.page-{id}.php
– If the page ID is 6, WordPress will look to usepage-6.php
.page.php
singular.php
index.php
Rendering category archive index pages uses the following path in WordPress:
category-{slug}.php
– If the category’s slug isnews
, WordPress will look forcategory-news.php
.category-{id}.php
– If the category’s ID is6
, WordPress will look forcategory-6.php
.category.php
archive.php
index.php
To display a tag archive index page, WordPress uses the following path:
tag-{slug}.php
– If the tag’s slug issometag
, WordPress will look fortag-sometag.php
.tag-{id}.php
– If the tag’s ID is6
, WordPress will look fortag-6.php
.tag.php
archive.php
index.php
Custom taxonomies use a slightly different template file path:
taxonomy-{taxonomy}-{term}.php
– If the taxonomy issometax
, and taxonomy’s term issometerm
, WordPress will look fortaxonomy-sometax-someterm.php.
In the case of post formats, the taxonomy is ‘post_format’ and the terms are ‘post-format-{format}. i.e.taxonomy-post_format-post-format-link.php
for the link post format.taxonomy-{taxonomy}.php
– If the taxonomy weresometax
, WordPress would look fortaxonomy-sometax.php
.taxonomy.php
archive.php
index.php
Custom Post Types use the following path to render the appropriate archive index page.
archive-{post_type}.php
– If the post type isproduct
, WordPress will look forarchive-product.php
.archive.php
index.php
(For rendering a single post type template, refer to the single post display section above.)
Based on the above examples, rendering author archive index pages is fairly explanatory:
author-{nicename}.php
– If the author’s nice name ismatt
, WordPress will look forauthor-matt.php
.author-{id}.php
– If the author’s ID were6
, WordPress will look forauthor-6.php
.author.php
archive.php
index.php
Date-based archive index pages are rendered as you would expect:
date.php
archive.php
index.php
Search results follow the same pattern as other template types:
search.php
index.php
Likewise, 404 template files are called in this order:
404.php
index.php
Rendering an attachment page (attachment
post-type) uses the following path:
{MIME-type}.php
– can be any MIME type (For example:image.php
,video.php
,pdf.php
). Fortext/plain
, the following path is used (in order):text-plain.php
plain.php
text.php
attachment.php
single-attachment-{slug}.php
– For example, if the attachment slug isholiday
, WordPress would look forsingle-attachment-holiday.php
.single-attachment.php
single.php
singular.php
index.php
The embed template file is used to render a post which is being embedded. Since 4.5, WordPress uses the following path:
embed-{post-type}-{post_format}.php
– First, WordPress looks for a template for the specific post. For example, if its post type ispost
and it has the audio format, WordPress would look forembed-post-audio.php
.embed-{post-type}.php
– If the post type isproduct
, WordPress would look forembed-product.php
.embed.php
– WordPress then falls back to embed.php
.- Finally, WordPress ultimately falls back to its own
wp-includes/theme-compat/embed.php
template.
Since WordPress 4.7, any dynamic part of a template name which includes non-ASCII characters in its name actually supports both the un-encoded and the encoded form, in that order. You can choose which to use.
Here’s the page template hierarchy for a page named “Hello World 😀” with an ID of 6
:
page-hello-world-😀.php
page-hello-world-%f0%9f%98%80.php
page-6.php
page.php
singular.php
The same behaviour applies to post slugs, term names, and author nicenames.
The WordPress template system lets you filter the hierarchy. This means that you can insert and change things at specific points of the hierarchy. The filter (located in the function) uses this filter name: "{$type}_template"
where $type
is the template type.
Here is a list of all available filters in the template hierarchy:
embed_template
404_template
search_template
frontpage_template
home_template
privacypolicy_template
taxonomy_template
attachment_template
single_template
page_template
singular_template
category_template
tag_template
author_template
date_template
archive_template
index_template
For example, let’s take the default author hierarchy:
author-{nicename}.php
author-{id}.php
author.php
To add author-{role}.php
before author.php
, we can manipulate the actual hierarchy using the ‘author_template’ template type. This allows a request for /author/username where username has the role of editor to display using author-editor.php if present in the current themes directory.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
function author_role_template( $templates = '' ) { $author = get_queried_object(); $role = $author ->roles[0]; if ( ! is_array ( $templates ) && ! empty ( $templates ) ) { $templates = locate_template( array ( "author-$role.php" , $templates ), false ); } elseif ( empty ( $templates ) ) { $templates = locate_template( "author-$role.php" , false ); } else { $new_template = locate_template( array ( "author-$role.php" ) ); if ( ! empty ( $new_template ) ) { array_unshift ( $templates , $new_template ); } } return $templates ; } add_filter( 'author_template' , 'author_role_template' ); |