In the first part of the series, we had a look at what template tags are, where they can be found, how they work, and how to use them.
In this second part, we're going to go through the first batch of the almost 200 template tags. Throughout the tutorial, we'll see template tags about post data: titles, dates, contents, and other meta data.
Getting & Displaying the Post Content: get_the_content()
& the_content()
They might be the most popular template tags of all time: These template tags return or echo the post content.
Parameters
Both template tags accept two parameters:
-
$more_link_text
(optional—string):
Text to replace the default "(more...)" text.
(Default: "(more…)") -
$strip_teaser
(optional—boolean):
Strip the teaser text.
(Default:FALSE
)
A note on the "teaser text": Did you know that there's a tag, <!--noteaser-->
, to make the content before <!--more-->
a "teaser text" and not show that part in the single post page even if $strip_teaser
is set to FALSE
? I certainly did not! It's not completely unknown, though—here's a post from 2009.
Usage
<?php // Get the post content. get_the_content(); // Display post content with a custom "read more" text. the_content( '– Read More »' ); ?>
Getting & Displaying the Post Content for Feeds: get_the_content_feed()
& the_content_feed()
These template tags get and output the content for feeds.
Parameters
Both template tags accept only one parameter:
-
$feed_type
(optional—string):
Type of the feed.
(Default: Default feed type)
Usage
<?php // Get the content for the default feed type. get_the_content_feed(); // Display the content for the "atom" feed type. the_content_feed( 'atom' ); ?>
Getting & Displaying the Excerpt of the Post: get_the_excerpt()
& the_excerpt()
These template tags fetch and display the excerpt of the post.
Parameters
These template tags don't accept any parameters.
Usage
<?php // Echoes the excerpt. the_excerpt(); // Returns the excerpt. $excerpt = get_the_excerpt(); ?>
Displaying the Excerpt of the Post for Feeds: the_excerpt_rss()
This template tag makes the post excerpt ready for feeds and outputs it.
Parameters
This template tag doesn't accept any parameters.
Usage
<?php the_excerpt_rss(); ?>
Getting & Displaying the Post Title: get_the_title()
& the_title()
These template tags let you return or echo the title of your posts.
Parameters
get_the_title()
accepts three parameters:
-
$post_ID
(optional—integer or object):
The ID of the post.
(Default: Current post)
And the_title()
accepts three parameters:
-
$before
(optional—string):
The text or HTML code to display before the output.
(Default: Empty) -
$after
(optional—string):
The text or HTML code to display after the output.
(Default: Empty) -
$echo
(optional—boolean):
Whether echo (TRUE
) or return (FALSE
) the tag.
(Default:TRUE
)
Usage
<?php // Get the current post's title with default options. $post_title = get_the_title(); // Display the current post's title wrapped with a H1 tag. the_title( '<h1>', '</h1>' ); ?>
Getting & Displaying the "Escaped" Post Title: the_title_attribute()
This template tag takes your post titles and makes them ready to be used in HTML attribute values. See the example for further explanation.
Parameters
This template tag accepts only one parameter:
-
$args
(optional—array):
An array of the following arguments:-
'before'
(string): HTML code to add before the output.
(Default: Empty) -
'after'
(string): HTML code to add after the output.
(Default: Empty) -
'echo'
(boolean): Whether to echo the template tag or not.
(Default:TRUE
) -
'post'
(object): Current post object to get the title from.
(Default: 0)
-
Usage
Let's take this title as an example:
Bill & Melinda Gates Spend Billions on Charity, Say "We Did???"
The title has two quotes and one ampersand that will mess up your HTML code if used inside an HTML attribute, because attributes are wrapped with quotes, too. That's where the_title_attribute()
comes in handy:
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
Now the link's title
attribute looks like this:
Bill & Melinda Gates Spend Billions into Charity, Say "We Did???"
And like this, it won't mess up the HTML with unescaped quotes and ampersands.
Displaying the Post Title for Feeds: the_title_rss()
This template tag gets your title, makes it ready for feeds, and displays it.
Parameters
This template tag doesn't accept any parameters.
Usage
<?php the_title_rss(); ?>
Getting & Displaying the Date of the Post: get_the_date()
& the_date()
These template tags fetch and echo the date of the post.
Parameters
get_the_date()
accepts two parameters:
-
$date_format
(optional—string):
The format of the date.
(Default: Date format set in the General Options page) -
$post_ID
(optional—integer or object):
The ID of the post.
(Default: Current post)
And the_date()
accepts four parameters:
-
$date_format
(optional—string):
The format of the date.
(Default: Date format set in the General Options page) -
$before
(optional—string):
The text or HTML code to display before the output.
(Default: Empty) -
$after
(optional—string):
The text or HTML code to display after the output.
(Default: Empty) -
$echo
(optional—boolean):
Whether echo (TRUE
) or return (FALSE
) the tag.
(Default:TRUE
)
Usage
<?php // Get the date with the default date format. get_the_date(); // Display the date with a different date format and some before & after content. the_date( 'Y-m-d', '<time>', '</time>' ); ?>
Getting & Displaying the Date of the Post in ISO 8601 Format: the_date_xml()
These template tags return and display the date of a post in ISO 8601 format.
Parameters
This template tag doesn't accept any parameters.
Usage
<?php the_date_xml(); ?>
Getting & Displaying the Time of the Post: get_the_time()
& the_time()
These template tags return or display the time of the post.
Parameters
get_the_time()
accepts two parameters:
-
$time_format
(optional—string):
The format of the time.
(Default: Time format set in the General Options page) -
$post_ID
(optional—integer or object):
The ID of the post.
(Default: Current post)
And the_time()
accepts only one parameter:
-
$time_format
(optional—string):
The format of the time.
(Default: Time format set in the General Options page)
Usage
<?php // Get current post's publish time with the default time format. $post_time = get_the_time(); // Display current post's publish time with a custom time format. the_time( 'H:i:s' ); ?>
Getting & Displaying the Author's Name: get_the_author()
& the_author()
These template tags get or display the name of the author of the post.
Parameters
These template tags don't accept any parameters.
Usage
<?php // Echoes the author name. the_author(); // Returns the author name. $author_name = get_the_author(); ?>
Displaying All the Meta Data of the Post: the_meta()
This template tag lists all the meta data of the post with an unordered list.
Parameters
This template tag doesn't accept any parameters.
Usage
<?php the_meta(); ?>
Getting & Displaying the Modification Date of the Post: get_the_modified_date()
& the_modified_date()
These template tags get and echo the last modification date of the post.
Parameters
get_the_modified_date()
accepts only one parameter:
-
$date_format
(optional—string):
The format of the date.
(Default: Date format set in the General Options page)
And the_modified_date()
accepts four parameters:
-
$date_format
(optional—string):
The format of the date.
(Default: Date format set in the General Options page) -
$before
(optional—string):
The text or HTML code to display before the output.
(Default: Empty) -
$after
(optional—string):
The text or HTML code to display after the output.
(Default: Empty) -
$echo
(optional—boolean):
Whether echo (TRUE
) or return (FALSE
) the tag.
(Default:TRUE
)
Usage
<?php // Get the last modification date with the default date format. get_the_modified_date(); // Display the last modification date with a different date format and some before & after content. the_modified_date( 'Y-m-d', '<time>', '</time>' ); ?>
Getting & Displaying the Modification Time of the Post: get_the_modified_time()
& the_modified_time()
These template tags fetch and output the modification time of the post.
Parameters
Both template tags accept only one parameter:
-
$time_format
(optional—string):
The format of the time.
(Default: Time format set in the General Options page)
Usage
<?php // Get the last modification time with the default time format. get_the_time(); // Display the last modification time of the post with a different time format. the_time( 'H:i:s' ); ?>
Getting & Displaying the Author Who Edited Current Post : get_the_modified_author()
& the_modified_author()
These template tags fetch and display the author who modified the post last.
Parameters
These template tags don't accept any parameters.
Usage
<?php the_modified_author(); get_the_modified_author(); ?>
Getting & Displaying a List of Pages of Paginated Posts: wp_link_pages()
You can split your posts into pages by using <!--nextpage-->
, and this template tag helps you list those pages.
Parameters
This template tag accepts only one parameter:
-
$args
(optional—array):
An array of the following arguments:-
'before'
(string): HTML code to add before the output.
(Default: '<p>Pages:') -
'after'
(string): HTML code to add after the output.
(Default: '</p>') -
'link_before'
(string): HTML code to add before each link.
(Default: '') -
'link_after'
(string): HTML code to add after each link.
(Default: '') -
'next_or_number'
(string): Whether to show page numbers ('number') or "next (or previous) page" links ('next').
(Default: 'number') -
'sep'
(string): Separator text.
(Default: ' ') -
'nextpagelink'
(string): Text for the "next page" link.
(Default: 'Next Page') -
'previouspagelink'
(string): Text for the "previous page" link.
(Default: 'Previous Page') -
'pagelink'
(string): Format of the links' anchor texts, if 'next_or_number' is set to 'number'. The "%" character will be replaced with the page number, so you can use a value like "Page %" to generate links like "Page 1, Page 2, Page 3".
(Default: '%') -
'echo'
(integer): Whether to echo the output (1) or return it (0).
(Default: 1)
-
Usage
<?php $args = array( // Custom HTML before & after output. 'before' => '<p>', 'after' => '</p>', // Custom separator. 'sep' => ' · ', // Custom page links. 'pagelink' => 'Part %', // Return the output instead of echoing. 'echo' => 0 ); $in_post_pagination = wp_link_pages( $args ); ?>
Conclusion
Hope you liked this first batch of template tags. There are seven more batches to go, so stay tuned for more template tags!
If you have any questions, comments or corrections, you can share your thoughts with us in the Comments section. And if you liked the article, don't forget to share it with your friends!
Comments