WordPress Cheatsheet
Below you will find snippets to help you through developing a wordpress template, hope it helps :)
get the home URL
<?php echo get_option('home'); ?>
display the blog description
<?php bloginfo('description'); ?>
display the blog name
<?php bloginfo('name'); ?>
get the home URL for template's directory
<?php bloginfo('template_directory') ?>
display the page list in descending order withour title
<?php wp_list_pages('sort_order=desc&title_li='); ?>
display the list of blogrolls
<?php wp_list_bookmarks(); ?>
within a loop
title of post
<?php the_title(); ?>
the text "read more" will not show up
<?php the_content(''); ?>
dispaly the tags of the post
<?php the_tags('', ' . ', ''); ?>
display the time of the current post
<?php the_time('F jS, Y') ?>
display the numeric ID of the current post
<?php the_ID(); ?>
display a link to them comment
<?php comments_popup_link('No Comments', '1 Comment', '% Comments'); ?>
display the author's name of the post
<?php the_author() ?>
display the unique numeric user ID for the author of a post
<?php the_author_ID()?>
display the commentor avatar in a size of 80px X 80px
<?php echo get_avatar( $comment, 80 ); ?>
display the comment content
<?php comment_text() ?>
display the URL for the permalink of the post currently being processed
<?php the_permalink() ?>
Paganavigation
display a link to the previous post with an anchor text as "Previous Post"
<?php previous_post_link('%link', 'Previous Post') ?>
display a link to the next post with an anchor text as "Next Post"
<?php next_post_link('%link', 'Next Post') ?>
display a link to the previous page with an anchor text as "Previous Page"
<?php posts_nav_link('','','« Previous Entries') ?>
display a link to the next page with an anchor text as "Next Page"
<?php posts_nav_link('','Next Entries »','') ?>
display a link to the previous image within the gallery
<?php previous_image_link() ?>
display a link to the previous image within the gallery
<?php next_image_link() ?>
file linking
load header.php
<?php get_header(); ?>
load sidebar.php
<?php get_sidebar(); ?>
load footer.php
<?php get_footer(); ?>
goes in footer.php
<?php wp_footer(); ?>
goes in header.php
<?php wp_head(); ?>
load comments.php
<?php comments_template(); ?>
load searchform.php
<?php include (TEMPLATEPATH . '/searchform.php'); ?>
load about.php
<?php include('about.php'); ?>
load featured-post.php
<?php include('featured-post.php'); ?>
widgetize sidebar
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('sidebar2') ) : ?>
if it is a widgetize sidebar, defince this as sidebar 2
<?php endif; ?>
define the page template name in admin panel
<?php /*
Template Name: Links
*/ ?>
normal post loop
Start loop
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
a normal posts loop, the contents go here
<?php endwhile; else: ?>
<?php endif; ?>
query post loop
Start loop
<?php $query_string = ...
<?php query_posts($query_string); while (have_posts()) : the_post(); ?>
the contents go here
<?php endwhile; ?>
comment loop
start the comments loop
<?php foreach ($comments as $comment) : ?>
the contents go here
<?php endforeach; ?>
end comments loop
use of custom fields
start the loop
<?php while (have_posts()) : the_post(); ?>
define the custom field key for the post being processed in the loop with the ID
<?php $photo = get_post_meta($post->ID, "myphoto", TRUE); ?>
display the custom field value
<?php echo $mydesc; ?>
<?php endwhile; ?>
Custom Post Types
Create Custom Post type
function codex_custom_init() {
$labels = array(
'name' => 'Books',
'singular_name' => 'Book',
'add_new' => 'Add New',
'add_new_item' => 'Add New Book',
'edit_item' => 'Edit Book',
'new_item' => 'New Book',
'all_items' => 'All Books',
'view_item' => 'View Book',
'search_items' => 'Search Books',
'not_found' => 'No books found',
'not_found_in_trash' => 'No books found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Books'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'book' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
);
register_post_type( 'book', $args );
}
add_action( 'init', 'codex_custom_init' );
Santize HTML Inputs
<?php
$sanitized_email = sanitize_email('éric@loremipsum.com!');
print $sanitized_email; // will output:'ric@loremipsum.com'
?>
<?php
// If you want to explicitly style a post, you can use the sanitized version of the post title as a class
$post_class = sanitize_html_class( $post->post_title );
echo '<div class="' . esc_attr( $post_class ) . '">';
<?php sanitize_title( $title, $fallback_title, $context ) ?>
<?php
$new_url = sanitize_title('This Long Title is what My Post or Page might be');
echo $new_url; // this-long-title-is-what-my-post-or-page-might-be
?>
Written by Austin Turnage
Related protips
2 Responses
Great, thank you! :)
Some notes:
"get the home URL
<?php echo get_option('home'); ?>
Better to use
<?php echo home_url(); ?>
You can even add stuff at the end of the URL: <?php echo home_url( '/goes/at/the/end/' ); ?>
Any of the includes - use get_template_part()
- this way templates can be overridden in a child theme. For instance to get about.php :
<?php get_template_part( 'about' ); ?>
Also to get the search form:
<?php get_search_form(); ?>