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('',', '); ?>
Show the tags WITHOUT A LINK, JUST TEXT:
<?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() ) : the_post_thumbnail('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
):
<?php if ( has_post_thumbnail()) { $full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full'); echo '<a href="' . $full_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >'; the_post_thumbnail('thumbnail'); 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>'); ?>
Reuben Chovuchovu
September 17th, 2013 at 1:07 pm
Thanks for a good piece. Quite handy for some of us just starting out in wordpress design and development.