Theme Template Tag Cheat Sheet

These are some of the common template tags you will use, listed by the file you would use it in. Some are appear more than once.

Right now it’s just a list. Eventually I hope to link them to their respective pages in the WordPress codex.

index.php

  • get_header() – runs header.php and inserts it into the page
  • get_sidebar() – runs sidebar.php and inserts it into the page
  • get_footer() – runs footer.php and inserts it into the page
  • have_posts() – returns true if there are posts and false if not. Used twice in if( have_posts() ) and while( have_posts() )
  • get_template_part('content') – runs content.php and inserts it into the page. Used inside the LOOP. you can get other files like this: to get content-video.php you would write get_template_part('content', 'video').

content.php

  • This is used if you want to separate out the loop from index.php. Otherwise all of this needs to go in the loop in index.php. All of these require being in the LOOP to run.
  • the_ID() – gets the ID of the post:
    <article id="post-<?php the_ID();?>" <?php post_class(); ?> >
  • post_class() - returns class=” xx ” where xx is a bunch of class names that relate to the post. See above.
  • the_title() – the title of the post
  • the_title_attribute()
  • the_permalink()
  • get_the_date()
  • the_date()
  • comments_open()
  • comments_popupLlink()
  • the_content(): shows the full content of the post or page.
  • the_excerpt(): this is used if you only want to show the excpert of the post and not the full post. Most of the time you use either the_content() or the_excerpt() and not both.
  • the_author_link()

header.php

  • bloginfo('name') – used in the title attribute in head and for the h1 for the site name in body
  • bloginfo('description') – used to get the Tag Line of the blog
  • wp_title() – used to show the title of the page in the title attribute
  •  get_template_directory_uri() – gets the path to the theme’s folder. Must put echo in front of it. Used to link in other CSS files besides style.css. It can also be used to pull in images, like for the header
  • bloginfo('stylesheet_url') – this is used in the href attribute of the link element to include style.css
  • bloginfo('description') – used to show the tagline, you set the tagline in Dashboard > General
  • wp_head() – add this in just before the closing </head>
  • wp_nav_menu() - used to pull in the menus that you can set in the Dashboard > Appearance > Menus

footer.php

  • echo date("Y");
  • wp_footer();
  • bloginfo(‘name’);

sidebar.php

  • dynamic_sidebar( 'home-sidebar' ); This gets sidebar with an id of home-sidebar.
  • is_active_sidebar('x') :x is replaced with the name of the sidebar. This is used to check if a sidebar is active. You can use this to only output a sidebar if there are widgets in it like so:
    <?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?> <div class="sidebar"> <?php dynamic_sidebar( 'sidebar-1' ); ?> </div><!-- #secondary --> <?php endif; ?> 
  • The rest of the code for the sidebar is in functions.php

functions.php

  • Functions.php is where you can write functions you’ll use in your theme. While your imagination is the limit here, for now I’ll focus on how to register a sidebar. While this shows registering two, you can register as many as you like and then use them throughout your theme pages.
  • There are two steps to registering a sidebar
    1. writing the function that registers the sidebar
    2. adding the action to make sure the register function runs at the right time

    below is example code for both steps

    Here we’ve put all of our register functions in one function named sidebars_init

     function sidebars_init() { //short version of named sidebar //this is what the is_active_sidebar code above would call register_sidebar(array('name'=>'Sidebar 1', 'id' => 'sidebar-1')); //longer version where the HTML that wraps the widget and the title is specified. register_sidebar( array( 'name' => 'Home Sidebar', 'id' => 'home-sidebar', 'before_widget' => '<div class="widget">', 'after_widget' => '</div><!-- end widget -->', 'before_title' => '<h3>', 'after_title' => '</h3>' ) ); //end register_sidebar } 

    And now we need to hook the sidebars_init function to widgets_init so it runs at the right time. It’s important to note that this code is NOT inside of the above function.

    //this makes sure the registration happens add_action( 'widgets_init', 'sidebars_init' ); 

single.php

  • This is (or can be) basically the same as index.php. The main difference is that you’re getting content for a single post. In this case we’re just going to change what template part we get, but you could make other changes if you like. The other difference is that you dont’ need the if(have_posts()) statement.
  • get_template_part('content', 'single'): gets the page that is shown next.

content-single.php

  • This is similar to content, but it is designed to show the content for a single post. The main change is that it adds the comments after the post.
  • comments_template('', true): that calls comments.php which is next

comments.php

  • have_comments(): this returns true if there are comments and false if there are not.
  • comments_number(): outputs the number of comments. It takes three arguments:1)text if no comments, 2) text if one comment, 3)text if more than one comment. Here is an example
    There <?php comments_number( 'are no comments','is one comment', ' are % comments' ) ?>

    The % is replaced with the number of comments so the above would output the following depending on the number of comments:

    • There are no comments
    • There is one comment
    • Ther are 23 comments
  • wp_list_comments(): this spits out the comments themselves. WP wraps it’s own HTML around them.
  • comment_form(): this spits out the form to allow people to make comments.
  • get_comment_pages_count(): this gets what it says.
  • get_option(‘page_comments’): this gets true/false if comments are on the current page
  • previous_comments_link(): gets a link to the previous page of comments when you’re paging through comments
    <?php previous_comments_link('← Older Comments'); ?>
  • next_comments_link(): gets a link to the next page of comments when you’re paging throug comments.
    <?php next_comments_link('← Newer Comments'); ?>

Leave a Reply