This is my WordPress cheat sheet, these are a list of codes I’ve collected over the years for custom theme development. I will try to add explanations as soon as I can, but for now, this serves as a reference sheet for me.
Blog/Theme Information
Direct link to the template directory: <?php bloginfo('template_directory'); ?>
Blog Name: <?php bloginfo('name'); ?>
Blog Description: <?php bloginfo('description'); ?>
Site Address*(as defined in General options): <?php echo home_url(); ?>
Site Address (as defined by server): <?php echo site_url(); ?>
--
NOTE: these do not output an ending "/" You must add one to the markup.
Conditional Statements
Homepage: <?php if (is_home()) { echo ' class="current"'; } ?>
Single: <?php if (is_single()) { echo ' class="current"'; } ?>
Template: <?php if (is_page_template('templatename.php') ) { echo ' class="current"'; } ?>
Pagename (quotes): <?php if (is_page('pagename')) { echo ' class="current"'; } ?>
Page ID (no quotes): <?php if (is_page(123)) { echo ' class="current"'; } ?>
Conditional Statement with a include function:
<?php if (is_page('pagename')) { @ require_once ("filename.htm"); } ?>
Conditional Statement with block markup
<?php if (is_home()) :?>
insert HTML...
<?php endif; ?>
WordPress Loops
General page, single, archive loop
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
// Insert HTML and WP tags here...
<?php endwhile; ?>
<?php else : ?>
<h1>Not Found</h1>
<p>Sorry, but you are looking for something that isn't here.</p>
<?php get_search_form(); ?>
<?php endif; ?>
Customized loop with pagination built in (functions.php must have code):
<?php $additional_loop = new WP_Query("showposts=15&paged=$paged"); ?>
<?php while ($additional_loop->have_posts()) : $additional_loop->the_post(); ?>
//insert HTML here...
<?php endwhile ?>
<?php kriesi_pagination("$additional_loop->max_num_pages"); ?>
<?php kriesi_pagination(); ?>
The archive, category and single require only this after the loop:
<?php kriesi_pagination(); ?>
Customized loop for sidebar posts (notice the end tag) this allows for if/conditional statements to work.
<?php query_posts('showposts=3'); ?>
<?php while (have_posts()) : the_post(); ?>
// Insert HTML and WP tags here...
<?php endwhile; wp_reset_query();?>
Get customized loop for pages, not posts
This snippet doesn’t currently allow for pagination. At least I haven’t found a code for pagination to work with this yet.
<?php query_posts(array(
'showposts' => -1, 'post_parent' => 15, 'post_type' => 'page',
'orderby' => 'menu_order', 'order' => 'ASC')); while (have_posts()) { the_post(); ?>
//insert content here....
<?php } ?>
Listing Categories
List categories in a ordered list:
<ul><?php wp_list_categories('orderby=name&title_li='); ?></ul>
List categories in a drop down menu:
<?php wp_dropdown_categories('show_option_none=Select category'); ?>
<script type="text/javascript"> <!--
var dropdown = document.getElementById("cat");
function onCatChange() { if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
location.href = "<?php echo get_option('home'); ?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
}} dropdown.onchange = onCatChange; --> </script>
List archives in a drop down menu:
<select name="archive-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
<option value=""><?php echo esc_attr( __( 'Select Month' ) ); ?></option>
<?php wp_get_archives( 'type=monthly&format=option&show_post_count=1' ); ?></select>
Listing WordPress Pages
Show children of a particular page ID. This will print something for all child pages:
<?php if ($post->post_parent == '40' ) { echo ' insert HTML here'; } ?>
List all children pages by ID, the time they were created, and only one layer deep:
<?php wp_list_pages ('sort_column=menu_order&title_li=&child_of=2&depth=1'); ?>
List all children of any parent page (if they have children) no specific ID (to put in a template):
<?php if($post->post_parent)
$children = wp_list_pages("sort_column=menu_order&title_li=&depth=1&child_of=".$post->post_parent."&echo=0");
else $children = wp_list_pages("sort_column=menu_order&title_li=&depth=1&child_of=".$post->ID."&echo=0");
if ($children) { ?>
<ul><?php echo $children; ?></ul>
<?php } ?>
Post Thumbnails:
This displays the size and removes the title tag (sometimes, titles are annoying):
<?php the_post_thumbnail('large', array('title' => '')); ?>
Get the post thumbnail URL only (chose from full, thumbnail, large, medium). The catch is, you can only do this once per. You can’t have two for one (I got an error when I did that):
<?php the_post_thumbnail_url('large'); ?>
If you want to get a post’s thumbnail outside of the loop, use this (as you can see, I added a custom width and height to the image, this is optional of course):
<?php echo get_the_post_thumbnail($post->ID, array(886,180)); ?>
Use the post thumbnail. But if there isn’t one, use a fallback image:
<?php if(has_post_thumbnail()) { the_post_thumbnail('thumbnail');} else {?>
<img src="<?php bloginfo('template_directory');?>/img/gray.gif" alt="No Image">
<?php }?>
WP Includes / Template Labeling
Category: <?php echo category_description(); ?>
Custom: <?php /* Template Name: Template Title */ get_header(); ?>
Header: <?php get_header(); ?>
Sidebar: <?php get_sidebar(); ?>
Footer: <?php get_footer(); ?>
Comment: <?php comments_template(); ?>
Search Form: <?php get_search_form(); ?>
Limit post word count automatically:
1. Put the following in a functions.php file:
function new_excerpt_more($post) {
return '… <a class="permalink" href="'. get_permalink($post->ID) . '">' . 'read more' . '</a>';}
add_filter('excerpt_more', 'new_excerpt_more');
function new_excerpt_length($length) {return 40;}add_filter('excerpt_length', 'new_excerpt_length');
2. Put this: <?php the_excerpt();?>
where you want the excerpt to go replacing the <?php the_content(); ?>
.
Or, limit the content by character count. Forget about the functions file, add the following to archives and blog templates:
<?php echo substr(get_the_excerpt(), 0,250); ?>…
<a class="permalink" href="<?php the_permalink() ?>">read more</a>
Post Tags (all must be within the loop)
Post title: <?php the_title() ?>
Parent title: <?php $parent_title = get_the_title($post->post_parent); echo $parent_title; ?>
Content: <?php the_content() ?>
Edit Link: <?php edit_post_link('edit'); ?>
Post ID: <?php the_ID(); ?>
Permalink: <?php the_permalink() ?>
Author Name: <?php the_author(); ?>
Category List: <?php the_category(', ') ?>
Post Tags: <?php if( function_exists('the_tags') )
the_tags(__(' — Tags: '), ', ',','); ?>
The Date and time Format
Used to display the date of the post, not the actual dynamic time of the day.
When you need the time, use <?php the_time(); ?>
code.
Examples:
----------------------------------------------------------
F j, Y g:i a November 6, 2010 12:50 am
F j, Y November 6, 2010
F, Y November, 2010
g:i a 12:50 am
g:i:s a 12:50:48 am
l, F jS, Y Saturday, November 6th, 2010
M j, Y @ G:i Nov 6, 2010 @ 0:50
Y/m/d at g:i A 2010/11/06 at 12:50 AM
Y/m/d at g:ia 2010/11/06 at 12:50am
Y/m/d g:i:s A 2010/11/06 12:50:48 AM
Y/m/d 2010/11/06
----------------------------------------------------------
The Date within a Heading tag:
...
<?php the_date('Y-m-d', '<h4>', '</h4>'); ?>
----------------------------------------------------------
Post the calendar date and time:
<?php the_time(get_option('date_format').', '.get_option('time_format')) ?>
Dynamic Menus
Menu without a <div>
container with menu ID and menu class:
<?php wp_nav_menu (array(
'container' => 'false',
'theme_location' => 'main-menu',
'menu_id' => 'main-menu',
'menu_class' => 'menu' )); ?>
If not already, the menu(s) must be registered and enabled in the functions.php
file. As you can see from the above and following codes, the 'theme location'
name(s) must match with each menu that is registered:
add_action( 'init', 'register_my_menus' );
function register_my_menus() {register_nav_menus(array(
'main-menu' => __( 'Main' ),
'second-menu' => __( 'Second' ),
'third-menu' => __( 'Third' )));}
Author Meta
Conditional container, if a user has filled out their description, show a bio on their entries:
<?php if ( get_the_author_meta( 'description' ) ) :?>
// Insert author meta here...
<?php endif; ?>
Get the author avatar, add an ID number after 'user_email'
if this is used outside of the loop. Other wise, the ID isn’t needed when used within the loop, this applies to all the following <?php the_author_meta(); ?>
codes.
<?php echo get_avatar( get_the_author_meta( 'user_email',2 ),
apply_filters( 'twentyten_author_bio_avatar_size', 100 ) ); ?>
Various Author Meta for every standard template accept the author.php
template:
Author Name: <?php get_the_author(); ?>
Author Description: <?php the_author_meta('description' ); ?>
Author Twitter: <?php the_author_meta('twitter'); ?>
Author Facebook: <?php the_author_meta('facebook'); ?>
Author Email: <?php the_author_meta('user_email'); ?>
Author Bio page link: <a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>">Link</a>
...
Other codes include (separated by comma)
...
user_login, user_pass, user_nicename, user_email, user_url, user_registered,
user_activation_key, user_status, display_name, nickname, first_name, last_name,
description, jabber, aim, yim, user_level, user_firstname, user_lastname,
user_description, rich_editing, comment_shortcuts, admin_color, plugins_per_page,
plugins_last_view, ID
The following codes are for the author.php
template. This code must start off every template in order for the following tags to work:
<?php $curauth = (get_query_var('author_name')) ?
get_user_by('slug', get_query_var('author_name')) :
get_userdata(get_query_var('author'));?>
The following codes will bring in data specifically for the author.php
template:
Avatar w/size: <?php echo get_avatar( $curauth->ID , 100 ); ?>
AIM: <?php echo $curauth->aim; ?>
Description: <?php echo $curauth->description; ?>
Display Name: <?php echo $curauth->display_name; ?>
First Name: <?php echo $curauth->first_name; ?>
ID: <?php echo $curauth->ID; ?>
Jabber: <?php echo $curauth->jabber; ?>
Last Name: <?php echo $curauth->last_name; ?>
Nickname: <?php echo $curauth->nickname; ?>
User Email: <?php echo $curauth->user_email; ?>
Login: <?php echo $curauth->user_login; ?>
Nicename: <?php echo $curauth->user_nicename; ?>
Registered: <?php echo $curauth->user_registered; ?>
URL: <?php echo $curauth->user_url; ?>
Yahoo IM: <?php echo $curauth->yim; ?>
Comments Template
The beginning code (for password protected commenting):
<?php if ( post_password_required() ) : ?>
<p><?php _e('Enter your password to view comments.'); ?></p>
<?php return; endif; ?>
If people made comments wrapper (required). All comments should be within this conditional statement:
<?php if ( have_comments() ) : ?>
// insert HTML here...
<?php endif; ?>
Comment heading and count, usually placed within <h4></h4>
tags. Goes right between the above code:
<?php comments_number(__('No Comments'), __('1 Comment'), __('% Comments')); ?>
<?php if ( comments_open() ) : ?><?php endif; ?>
The actual user comment wrapper, to be placed after the above code and within the comments wrapper:
<?php foreach ($comments as $comment) : ?>
// put people's comments within here...
<?php endforeach; ?>
Comment Author Meta to go within the above code:
Get the avatar: <?php echo get_avatar( $comment, $size = '50' ); ?>
Author URL: <?php comment_author_link(); ?>
ID (used w/anchors): <?php comment_ID() ?>
Time: <?php comment_time() ?>
Date: <?php comment_date() ?>
The content: <?php comment_text() ?>
Edit Link: <?php edit_comment_link(__("edit")); ?>
Awaiting moderation message (usually placed above the comment text, after the author name:
<?php if ( $comment->comment_approved == '0' ) : ?>
<p><i><?php _e( 'Your comment is awaiting approval.'); ?></i></p>
<?php endif; ?>
The comment form section, placed after the user comments (it does not go within the comment list section):
<?php if ( comments_open() ) : ?>
<?php if ( get_option('comment_registration') && !is_user_logged_in() ) : ?>
<p><?php printf(__('You must be <a href="%s">logged in</a> to post a comment.'), wp_login_url( get_permalink() ) );?></p>
<?php else : ?>
<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post">
<?php if ( is_user_logged_in() ) : ?>
<span class="small">You're <?php printf(__('logged in as %s.'),
'<a href="'.get_option('siteurl').'/wp-admin/profile.php">'.$user_identity.'</a>'); ?>
<a href="<?php echo wp_logout_url(get_permalink()); ?>" title="Log out of this account">Log out</a>.</span>
<?php else : ?>
<label for="author">Name:<?php if ($req) _e('*'); ?>
<input type="text" name="author" id="author" value="<?php echo esc_attr($comment_author); ?>"/>
</label>
<label for="email">Email:<?php if ($req) _e('*'); ?>
<input type="email" name="email" id="email" value="<?php echo esc_attr($comment_author_email); ?>" />
</label>
<label for="url">Website:
<input type="url" id="url" name="url" value="<?php echo esc_attr($comment_author_url); ?>" />
</label>
<?php endif; ?>
<textarea name="comment">Be kind, keep it clean and respect everyone. Write your comment here...</textarea><br/>
<input name="Post" type="submit" value="Post Comment" title="Post Your Comment" class="submit-comment" />
<input type="hidden" name="comment_post_ID" value="<?php echo $id; ?>" />
<?php do_action('comment_form', $post->ID); ?>
</form>
<?php endif; // If registration required and not logged in ?>
<?php else : // Comments are closed ?>
<p><?php _e('---'); ?></p>
<?php endif; ?>
Search form code:
<form role="search" method="get" id="searchform" action="<?php bloginfo('home'); ?>/">
<input type="text" value="Search" name="s" id="s" class="field search tip" title="Type what you're looking for and press Enter"/>
</form>
Archives Template Page (Archives, Tags, Categories):
<?php if (have_posts()) : ?>
<?php /* If this is a category archive */ if (is_category()) { ?>
<h1>Category <?php echo single_cat_title(); ?></h1>
<?php /* If this is a tag archive */ } elseif ( function_exists ('is_tag') && (is_tag()) ) { ?>
<h1>Post Tagged <?php echo single_tag_title(); ?></h1>
<?php /* If this is a daily archive */ } elseif (is_day()) { ?>
<h1>Archive for <?php the_time('jS F Y'); ?></h1>
<?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
<h1>Archive for <?php the_time('F Y'); ?></h1>
<?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
<h1>Archive for <?php the_time('Y'); ?></h1>
<?php /* If this is an author archive */ } elseif (is_author()) { ?>
<h1>Author Archive</h1>
<?php } ?>
<?php while (have_posts()) : the_post(); ?>
// insert HTML...
<?php endwhile; ?>
<?php else : ?>
<h1>Not Found</h1>
<p>Sorry, but you are looking for something that isn't here.</p>
<?php get_search_form(); ?>
<?php endif; ?>
<?php kriesi_pagination(); ?>