THE BASICS

Template Include Tags
<?php get_header(); ?> <!--Gets Header Template-->
<?php get_sidebar(); ?> <!--Gets Sidebar Template-->
<?php get_footer(); ?> <!--Gets Footer Template-->
<?php comments_template(); ?> <!--Gets Comments Template-->
Conditional Tags

<?php is_home() ?> <!--when the user is on the home page(blog)-->
<?php is_front_page() ?> <!--when the user is on the home page (blog or page)-->
<?php is_single ?> <!--when a single post is displayed-->
<?php is_sticky() ?> <!--check if a post is sticky-->
<?php is_page() ?> <!--when a page is displayed-->
<?php is_category() ?> <!--when a category is displayed-->
Common Tags

<?php the_time() ?> <!--- Displays the time of the current post-->
<?php the_date() ?> <!--- Displays the date of a post or set of posts-->
<?php the_title(); ?> <!--- Displays or returns the title of the current post-->
<?php the_excerpt(); ?> <!--- Displays or returns the excerpt of the current post-->
<?php the_permalink() ?> <!--- Displays the URL for the permalink-->
<?php the_category() ?> <!--- Displays the category of a post-->
<?php the_author(); ?> <!--- Displays the author of the post-->
<?php the_ID(); ?> <!--- Displays the numeric ID of the current post-->
<?php wp_list_pages(); ?> <!--- Displays all the pages-->
<?php wp_tag_cloud(); ?> <!--- Displays a tag cloud-->
<?php wp_list_cats(); ?> <!--- Displays the categories-->
<?php get_calendar(); ?> <!--- Displays the calendar-->
<?php wp_get_archives() ?> <!--- Displays a date-based archives list-->
<?php posts_nav_link(); ?> <!--- Displays Previous page and Next Page links-->
<?php next_post_link() ?> <!--- Displays Newer Posts link-->
<?php previous_post_link() ?> <!--- Displays previous link-->
<?php get_stylesheet_directory_uri(); ?> <!-- Gets Stylesheet Directory -->
Template Bloginfo Tags
<?php bloginfo('name'); ?> <!--Title of the blog-->
<?php bloginfo('charset'); ?> <!--Displays the character set-->
<?php bloginfo('description'); ?> <!--Displays the description of the blog-->
<?php bloginfo('url'); ?> <!--Displays the address of the blog-->
<?php bloginfo('rss2_url'); ?> <!--Displays the RSS URL-->
<?php bloginfo('template_url'); ?> <!--Displays the URL of the template-->
<?php bloginfo('pingback_url'); ?> <!--Displays the pingback URL-->
<?php bloginfo('stylesheet_url'); ?> <!--Displays the URL for the template's CSS file-->
<?php bloginfo('wpurl'); ?> <!--Displays URL for WordPress installation-->
The Loop
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>
//Custom HTML & PHP code
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
Basic Theme Structure
  • header.php – header section
  • index.php – main section
  • sidebar.php – sidebar section
  • footer.php – footer section
  • single.php – post template
  • page.php – page template
  • comments.php – comments template
  • search.php – search content
  • searchform.php – search form
  • archive.php – archive
  • functions.php – special functions
  • 404.php – error page
Additional Resources

BASIC SNIPPETS

Time to Cook

Get Featured Image
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID, 'thumbnail') ); ?>
Hooks

This goes in your functions.php file.

Sometimes you want to use hooks to avoid editing core WordPress or whatever framework you’re using. WordPress has its own set of hooks, as does WooCommerce, WooThemes like Canvas, and various other frameworks we can utilize to avoid messing everything up. The below example is using a WooThemes hook.

add_action( 'woo_header_inside', 'fubar' );

function fubar () {

//what to display or do

}
Create a Menu Area

This goes in your functions.php file

function register_my_menus() {
register_nav_menus(
array(
'footer-menu' => __( 'Footer Menu' )
)
);
}
add_action( 'init', 'register_my_menus' );
Display a Menu Area

Just like with widgets, we have two main ways to display our custom menus, inline and with hooks. Below is inline:

<?php wp_nav_menu( array( 'theme_location' => 'footer-menu' )); ?>

Hook:

add_action( 'woo_content_before', 'menu_func' );

function menu_func() {
wp_nav_menu( array( 'theme_location' => 'footer-menu' ));
}
Create a Widget

This goes in the functions.php file

if (function_exists('register_sidebar')) {
register_sidebar(array(
'name' => 'whatever',
'description' => 'Whatever',
'before_widget' => '<div class="whatever">',
'after_widget' => '</div>',
'before_title' => '',
'after_title' => ''
));
}
Display a Widget

There are two main ways to display a widget. The first like the inline addition below to whichever template file we need it to display:

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('whatever') ) : ?>
<?php endif; ?>

The second is to create a hook, in the example below I am using a WooThemes hook:

add_action( 'woo_content_before', 'whatever_func' );

function whatever_func() {
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('whatever') ) : endif;
}
Create a Shortcode

Creates shortcode [foobar]


function foobar_func( $atts ){
return "foo and bar";
}
add_shortcode( 'foobar', 'foobar_func' );
Creating Custom Post Types & Custom Fields

In all honesty, it’s much easier to create custom post types & taxonomies using Custom Post Type UI than it is to hard code it; the same applies for custom fields. Using Advance Custom Fields is a lot easier.

But if you must know how to code it here are two examples of a Custom Post Type and Custom Field:

Custom Post Type

// Register Custom Post Type
function custom_post_type() {

$labels = array(
'name' => _x( 'Post Types', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Post Type', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Post Type', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'all_items' => __( 'All Items', 'text_domain' ),
'view_item' => __( 'View Item', 'text_domain' ),
'add_new_item' => __( 'Add New Item', 'text_domain' ),
'add_new' => __( 'Add New', 'text_domain' ),
'edit_item' => __( 'Edit Item', 'text_domain' ),
'update_item' => __( 'Update Item', 'text_domain' ),
'search_items' => __( 'Search Item', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
);
$args = array(
'label' => __( 'post_type', 'text_domain' ),
'description' => __( 'Post Type Description', 'text_domain' ),
'labels' => $labels,
'supports' => array( ),
'taxonomies' => array( 'category', 'post_tag' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
);
register_post_type( 'post_type', $args );

}

// Hook into the 'init' action
add_action( 'init', 'custom_post_type', 0 );

Custom Field

add_post_meta($id, '_name', 'value');
Get All Custom Fields
<?php the_meta(); ?>
Get Specific Custom Field
<?php echo get_post_meta($post->ID, 'custom_field_title', true); ?>
Display Shortcode in Template
 <?php echo do_shortcode('[your_short_code"]') ?> 
Include Custom Template File
<?php include (TEMPLATEPATH . '/template_file.php'); ?>
Custom Post Type Query With Pagination

Queries Post Type

<?php
query_posts(array(
'post_type' => 'CPT',
'showposts' => -1,
'meta_query' => array( array(
'key' => 'your_meta',
'value' => 'your_value', ))
) );

?>

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

<?php endwhile;?>

Displays Pagination

<?php
$count_post = $count_posts->publish / $show_posts;

if( $count_posts->publish % $show_posts == 1 ) {
$count_post++;
$count_post = intval($count_post);
};

for($i = 1; $i <= $count_post ; $i++) { ?>
<a <?php if($req_uri[1] == $i) { echo 'class=active_page'; } ?> href="<?php echo $uri . $i; ?>"><?php echo $i; ?></a>
<?php }
?>
Reset WordPress Query

Sometimes we’ll run a query, like the one above, and it will keep running down the page. So if I call the_title() it will not display the page title, but instead will display the title of something in the query. To stop this we need to reset the Wordpess query with the snippet below:

<?php wp_reset_query(); ?>
Register Script

Though you could drop the script inline, you can also just register the script through functions.php

// Register Script
function custom_scripts() {

wp_register_script( 'scriptname', 'scripturl', array( 'handles', 'to', 'load', 'before', 'script' ), '2.0', false );

}

// Hook into the 'wp_enqueue_scripts' action
add_action( 'wp_enqueue_scripts', 'custom_scripts' );
Deregister Script

If you want to de-register a WordPress core script and use a new version, such as Jquery, use the below:

<?php wp_deregister_script( 'jquery' ); ?>

Advanced Custom Fields

Advanced Custom Fields is a plugin, but a very powerful one. They have good documentation, but below are just a few things that I’ve caught myself using often. Additional documentation here.

Grab A Field

This applies to virtually every out-of-the-box field in ACFs. That includes images, attachments, text, WYSIWYG, etc. The output is actually specified within the field you create itself. You can specify that it output a URL, the actual HTML, etc, depending on your needs.

<?php the_field('your_field_name'); ?>
Display Gallery (requires Add-On)

This requires this add-on. This not only calls the gallery, but also calls Simple LightBox (a plugin) so that your gallery images open in a light box.

<?php
$image_ids = get_field('gallery', false, false);
$shortcode = '
[ gallery columns="4" ids="' . implode(',', $image_ids) . '"]
';
$gallery = do_shortcode( $shortcode );
echo slb_activate($gallery);
?>

No gaps between [ and g when you do code this. it will be [g…”]

Displays a Slider (requires Add-On)

This requires both the gallery plugin for ACFs, as well FlexiSlider from WooThemes

<?php

$images = get_field('images');

if( $images ): ?>
<div id="slider" class="flexslider">
<ul class="slides">
<?php foreach( $images as $image ): ?>
<li>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<p><?php echo $image['caption']; ?></p>
</li>
<?php endforeach; ?>
</ul>
</div>

<?php endif; ?>

Recommended Plug-ins & Frameworks