Quick Reference – Template Tags To Use In The WordPress Loop

These template tags must be used within the loop, unless otherwise indicated. Some of these tags can be used outside the loop if on single.php, cateogory.php, or archives.php, when indicated below.

Display the value of a custom metabox field, in which ‘key’ is your custom meta key:

<?php echo get_post_meta($post->ID, 'key', true); ?>

The Title of a Post linking to its Permalink:

<a href="<?php the_permalink(); ?>" 
title="<?php the_title_attribute(); ?>">
<?php the_title(); ?></a>

Show the Category (or categories, separated by a comma) of a post:

<?php the_category(', '); ?>

Show the Tags of a post linking to more posts with that tag. This will output the word “Tags:” before the actual tags:

<?php the_tags(); ?>

To output just the tag links without the label “Tags:” use:

<?php the_tags('',', '); ?>


<?php $posttags = get_the_tags();
if ($posttags) {foreach($posttags as $tag) {echo $tag->name .', '; } }?>

Get Terms of a custom taxonomy WITHOUT LINK, JUST TEXT. In this example, the custom taxonomy is ‘gender’:

$terms_as_text = strip_tags( get_the_term_list( $wp_query->post->ID, 'gender', '', ', ', '' ) );

Display the name of the author of a post, without a link, just the text:

<?php the_author(); ?>

Display the name of the author of a post, with a link to all posts by that author:

<?php the_author_posts_link(); ?>

Display the featured image thumbnail without a link (can be used outside the loop on single.php):

<?php if ( has_post_thumbnail() ) : 
endif; ?>

Display the featured image thumbnail with a link to the post’s permalink:

<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" 
title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?></a>
<?php endif; ?>

Display the featured image thumbnail with a link to the full size image (good for using a pretty lightbox) (can be used outside the loop on single.php):

 if ( has_post_thumbnail()) {
   $full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full');
   echo '<a href="' . $full_image_url&#91;0&#93; . '" title="' . the_title_attribute('echo=0') . '" >';
   echo '</a>';

Add a CSS class to the post thumbnail. Replace ‘alignleft’ below with whatever CSS class you need:

<?php the_post_thumbnail('thumbnail', array('class' => 'alignleft')); ?>

Display the date in the default format set your WordPress blog settings:

<?php the_date(); ?>

Display only the month and day, without the year:

<?php the_time('F j'); ?>

Display only the year surrounded with itemprop “copyrightYear” microdata:

<?php the_date('Y','<span itemprop="copyrightYear">', '</span>'); ?>

See more: , ,


We've One Response

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this: