WordPress V5.0+Template Tag Reference Guide as of Version 5.2
Presented by DBS > Interactive Development :
Post Tags
Body Classbody_class();
If no parameter is passed, this tag builds a predictably-structured body CSS class depending on the context of the page or post. If you do pass a parameter, it assigns that value or values as a class for that body tag. This can allow you much finer control over CSS coding, as it gives you more targeted and specific class-naming information you can hook into for styling. For instance, if you wanted to style post ID 43 with a blue background, but leave all others without a background, using this tag in your template allows you to simply add a rule to your CSS to control this: body.postid-43 { background-color: #0000FF; }. This tag is most often used in the header.php's HTML body tag.
The echoed class attribute may include one or more of the following values for the class attribute, dependent upon the pageview:
- rtl
- home
- blog
- archive
- date
- search
- paged
- attachment
- error404
- single postid-(id)
- single-(post_type)
- page-id-(page_id)
- attachmentid-(id)
- attachment-(mime-type)
- author
- author-(user_nicename)
- category
- category-(slug)
- tag
- tag-(slug)
- page-parent
- page-child parent-pageid-(id)
- page-template page-template-(template file name)
- search-results
- search-no-results
- logged-in
- paged-(page number)
- single-paged-(page number)
- page-paged-(page number)
- category-paged-(page number)
- tag-paged-(page number)
- date-paged-(page number)
- author-paged-(page number)
- search-paged-(page number)
- tax-(taxonomy name) (since 3.1)
- term-(term name) (since 3.1)
- admin-bar (since 3.1)
- custom-background (since 3.3)
<?php body_class($class); ?>
$class
String/Array • Default: none
Optional. Can be either an array of class names or a string with space-separated classes names.
Get Archives Linkget_archives_link();
Retrieve archive link content based on predefined or custom code. The format can be one of four styles. The 'link' for head element, 'option' for use in the select element, 'html' for use in list (either ol or ul HTML elements). Custom content is also supported using the before and after parameters. The 'link' format uses the link HTML element with the archives relationship. The before and after parameters are not used. The text parameter is used to describe the link. The 'option' format uses the option HTML element for use in select element. The value is the url parameter and the before and after parameters are used between the text description. The 'html' format, which is the default, uses the li HTML element for use in the list HTML elements. The before parameter is before the link and the after parameter is after the closing link. The custom format uses the before parameter before the link ('a' HTML element) and the after parameter after the closing link tag. If the above three values for the format are not used, then custom format is assumed.
<?php get_archives_link($url, $text, $format, $before, $after); ?>
$url
String • Default: none
URL to archive.
$text
String • Default: none
Archive text description.
$format
String • Default: 'html'
Optional. Can be 'link', 'option', 'html', or custom.
$before
String • Default: ''
Optional. Text before the link.
$after
String • Default: ''
Optional. Text after the link.
Get Delete Post Linkget_delete_post_link();
Returns URL to move a post to the trash as string value. Can be used within the WordPress loop or outside of it. Can be used with pages, posts, attachments, and revisions.
<?php get_delete_post_link($id, $force_delete); ?>
$id
Integer • Default: none
Optional. The post ID.
$force_delete
Boolean • Default: false
Optional. Whether to bypass trash and force deletion.
Get Edit Post Linkget_edit_post_link();
Returns edit post url as string value. Can be used within the WordPress loop or outside of it. Can be used with pages, posts, attachments, and revisions.
<?php get_edit_post_link($id, $context); ?>
$id
Integer • Default: none
Optional. The post ID.
$context
String • Default: display
Optional. The default to display. How to write the '&', defaults to '&'.
Next Image Linknext_image_link();
This creates a link to the next image attached to the current post.
<?php next_image_link($size, $text); ?>
$size
String/Array • Default: thumbnail
Optional. Size of image, either array or string. 0 or none will default to post_title or $text.
$text
String • Default: false
Optional. If included, link will reflect $text variable.
Next Post Linknext_post_link();
Used on single post permalink pages, this template tag displays a link to the next post which exists in chronological order from the current post. This tag must be used in The Loop.
<?php next_post_link($format, $link, $in_same_cat, $excluded_categories); ?>
$format
String • Default: '%link »'
Optional. Format string for the link. This is where to control what comes before and after the link. '%link' in string will be replaced with whatever is declared as 'link' (see next parameter). 'Go to %link' will generate "Go to < href=..." Put HTML tags here to style the final results.
$link
String • Default: '%title' (next post's title)
Optional. Link text to display.
$in_same_cat
Boolean • Default: false
Optional. Indicates whether next post must be within the same category as the current post. If set to 'true', only posts from the current category will be displayed. If the post is in both the parent and subcategory, or more than one category, the next post link will lead to the next post in any of those categories.
$excluded_categories
String/Array • Default: none
Optional. Array or a comma-separated list of numeric category IDs from which the next post should not be listed. Note: Previous versions of WordPress used to have IDs separated by " and ". Example: '1 and 5 and 15'
Next Posts Linknext_posts_link();
Prints a link to the next set of posts within the current query. Because post queries are usually sorted in reverse chronological order, next_posts_link() usually points to older entries (toward the end of the set) and prev_posts_link() usually points to newer entries (toward the beginning of the set).
<?php next_posts_link($label, $max_pages); ?>
$label
String • Default: 'Next Page »'
Optional. The link text.
$max_pages
Integer • Default: 0
Optional. Limit the number of pages on which the link is displayed. The default value "0" means "no limit".
Post Classpost_class();
WordPress theme authors who want to have finer css control options for their post styling have the post_class function available. When the post_class function is added to a tag within the loop, for example <div id="post" <?php post_class(); ?> >, it will print out and add various post-related classes to the div tag. This function is typically used in the index.php, single.php, and other template files that feature hierarchical post listings.
The post_class may include one or more of the following values for the class attribute, dependent upon the pageview:
- .post-id
- .post
- .attachment
- .sticky
- .hentry (hAtom microformat pages)
- .category-ID
- .category-name
- .tag-name
- .format-name
<?php post_class(); ?>
No parameters.
Default Values
The post_class CSS classes appear based upon the post pageview Conditional Tags as follows.
Front Page
If posts are found on the front page of the blog, be it static or not,
the class selectors are:
post post-id hentry
Single Post
Single post template files and pageviews feature the class selectors:
post post-id hentry
Sticky Post
Posts designated as "sticky," sticking to the front page of the blog, feature the class selector:
sticky
Author
Author template files and pageviews displaying posts feature the class selectors:
post post-id
Category
Category template files and pageviews displaying posts feature the class selectors:
post post-id category-ID category-name
Tags
Tag template files and pageviews with posts feature the class selectors:
tag-name post post-id
Archives
Archive pageviews and pageviews with posts feature CSS classes:
post post-id
Search
Search template files and pageviews with posts feature the class selectors:
post post-id
Attachment
Attachment template files and pageviews feature the class selectors:
attachment post post-id
Format
Posts using Post Formats feature the class selector:
format-name
Post Password Requiredpost_password_required();
Whether post requires password and correct password has been provided. Returns false if a password is not required or the correct password cookie is present, true otherwise.
<?php post_password_required($post); ?>
$post
Integer or Object • Default: none
Either the post ID (integer) or the post object.
Post Type Archive Titlepost_type_archive_title();
Displays or returns the title of the post when on a single post page (permalink page). This tag can be useful for displaying post titles outside The Loop.
<?php post_type_archive_title($prefix, $display); ?>
$prefix
String • Default: none
Optional. Text to place before the title.
$display
Boolean • Default: true
Optional. Should the title be displayed (true) or returned for use in php (false).
Posts Nav Linkposts_nav_link();
Displays links for next and previous pages. Useful for providing "paged" navigation of index, category and archive pages. For displaying next and previous pages of posts see instead next_posts_link() and previous_posts_link(). For displaying next and previous post navigation on individual posts, see instead next_post_link() and previous_post_link().
<?php posts_nav_link($sep, $prelabel, $nextlabel); ?>
$sep
String • Default: ' — '
Text displayed between the links.
$prelabel
String • Default: '« Previous Page'
Link text for the previous page.
$nextlabel
String • Default: 'Next Page »'
Link text for the next page.
Previous Image Linkprevious_image_link();
This creates a link to the previous image attached to the current post.
<?php previous_image_link($size, $text); ?>
$size
String/Array • Default: 'thumbnail'
Optional. Size of image, either array or string. 0 or none will default to post_title or $text.
$text
String • Default: false
Optional. If included, link will reflect $text variable.
Previous Post Linkprevious_post_link();
Used on single post permalink pages, this template tag displays a link to the previous post which exists in chronological order from the current post. This tag must be used in The Loop
<?php next_post_link($format, $link, $in_same_cat, $excluded_categories); ?>
$format
String • Default: '« %link'
Optional. Format string for the link. This is where to control what comes before and after the link. '%link' in string will be replaced with whatever is declared as 'link' (see next parameter). 'Go to %link' will generate "Go to < href=..." Put HTML tags here to style the final results.
$link
String • Default: Previous post's title '%title'
Optional. Link text to display.
$in_same_cat
Boolean • Default: 0 (false)
Optional. Indicates whether previous post must be within the same category as the current post. If set to 'true', only posts from the current category will be displayed.
$excluded_categories
String • Default: none
Optional. Numeric category ID(s) from which the previous post should not be listed. Separate multiple categories with and; example: '1 and 5 and 15'.
Previous Posts Linkprevious_posts_link();
Prints a link to the previous set of posts within the current query. Because post queries are usually sorted in reverse chronological order, next_posts_link() usually points to older entries (toward the end of the set) and prev_posts_link() usually points to newer entries (toward the beginning of the set).
<?php previous_posts_link($label, $max_pages); ?>
$label
String • Default: '« Previous Page'
Optional. The link text.
$max_pages
Integer • Default: 0
Optional. Limit the number of pages on which the link is displayed. The default value "0" means "no limit".
Single Post Titlesingle_post_title();
Displays or returns the title of the post when on a single post page (permalink page). This tag can be useful for displaying post titles outside The Loop.
<?php single_posts_title($prefix, $display); ?>
$prefix
String • Default: none
Optional. Text to place before the title.
$display
Boolean • Default: true
Optional. Should the title be displayed (true) or returned for use in php (false).
Sticky Classsticky_class();
Displays the Sticky Posts class on a post if applicable. This tag must be within The Loop.
<?php sticky_class(); ?>
No parameters.
The Categorythe_category();
Displays a link to the category or categories a post belongs to. This tag must be used within The Loop.
<?php the_category($separator, $parents, $post_id); ?>
$separator
String • Default: ''
Optional. Text or character to display between each category link. By default, the links are placed in an HTML unordered list. An empty string will result in the default behavior.
$parents
String • Default: ''
Optional. How to display links that reside in child (sub) categories.
Options are:
'multiple' - Display separate links to parent and child categories, exhibiting "parent/child" relationship.
'single' - Display link to child category only, with link text exhibiting "parent/child" relationship.
Note: Default is a link to the child category, with no relationship exhibited.
$post_id
Integer • Default: false
Optional. Post ID to retrieve categories. The default value false results in the category list of the current post.
The Category RSSthe_category_rss();
Display the post categories in the feed.
<?php the_category_rss($type); ?>
$type
String • Default: 'rss'
Optional. Either 'rss', 'atom', or 'rdf'.
The Contentthe_content();
Displays the contents of the current post. This tag must be within The_Loop.
If the quicktag <!--more--> is used in a post to designate the "cut-off" point for the post to be excerpted, the_content() tag will only show the excerpt up to the <!--more--> quicktag point on non-single/non-permalink post pages. By design, the_content() tag includes a parameter for formatting the <!--more--> content and look, which creates a link to "continue reading" the full post.
Notes about <!--more-->
- No whitespaces are allowed before the "more" in the <!--more--> quicktag. In other words <!-- more --> will not work!
- The <!--more--> quicktag will not operate and is ignored in Templates where just one post is displayed, such as single.php.
<?php the_content($more_link_text, $stripteaser); ?>
$more_link_text
String • Default: '(more...)'
Optional. The link text to display for the "more" link.
$stripteaser
Boolean • Default: false
(Optional) Whether to put strip teaser content before the "more" link.
The Content RSSthe_content_rss();
Display the post content for the feed.
For encoding the html or the $encode_html parameter, there are three possible values. '0' will make urls footnotes and use make_url_footnote(). '1' will encode special characters and automatically display all of the content. The value of '2' will strip all HTML tags from the content.
Also note that you cannot set the amount of words and not set the html encoding. If that is the case, then the html encoding will default to 2, which will strip all HTML tags.
To restrict the amount of words of the content, you can use the cut parameter. If the content is less than the amount, then there won't be any dots added to the end. If there is content left over, then dots will be added and the rest of the content will be removed.
<?php the_content_rss($more_link_text, $stripteaser, $more_file, $cut, $encode_html); ?>
$more_link_text
String • Default: 'more...'
Optional. Text to display when more content is available but not displayed.
$stripteaser
Boolean • Default: false
Optional. Whether to put strip teaser content before the more text.
$more_file
String • Default: ''
Optional. Apparently not used at this time.
$cut
Integer • Default: 0
Optional. Amount of words to keep for the content.
$encode_html
Integer • Default: 0
Optional. How to encode the content.
The Excerptthe_excerpt();
Displays the excerpt of the current post with [...] at the end, which is not a "read more" link. If you do not provide an explicit excerpt to a post (in the post editor's optional excerpt field), it will display an automatic excerpt which refers to the first 55 words of the post's content. Also in the latter case, HTML tags and graphics are stripped from the excerpt's content. This tag must be within The Loop.
Note: If the current post is an attachment, such as in the attachment.php and image.php template loops, then the attachment caption is displayed. Captions do not include the excerpt [...] marks.
Sometimes it is more meaningful to use only the_content() function. the_content() will decide what to display according to whether <!--more--> tag was used. The <!--more--> tag splits post/page into two parts: only content before the tag should be displayed in listing. Remember that <!--more--> is (of course) ignored when showing single post/page.
<?php the_excerpt(); ?>
No parameters.
The Excerpt RSSthe_excerpt_rss();
Display the post excerpt for the feed.
<?php the_excerpt_rss(); ?>
No parameters.
The IDthe_ID();
Displays the numeric ID of the current post. This tag must be within The Loop. Note: This function displays the ID of the post, to return the ID use get_the_ID().
<?php the_ID(); ?>
No parameters.
The Metathe_meta();
This is a simple built-in function for displaying custom fields for the current post, known as the "post-meta" (stored in the wp_postmeta table). It formats the data into an unordered list. It must be used from within The Loop or in a theme file that handles data from a single post (e.g. single.php). the_meta() will ignore meta_keys (i.e. field names) that begin with an underscore.
<?php the_meta(); ?>
No parameters.
The Shortlinkthe_shortlink();
Used on single post permalink pages, this template tag displays a "URL shortening" link for the current post. By default, this will mean the URL has a format of /?p=1234, and will only appear if pretty permalinks are enabled.
However, this feature is limited by design and intended to be leveraged by plugins that may offer links in a different format, a custom format, or even a format provided by a third-party URL shortening service. Refer to get_permalink() if you want to return the permalink to a post for use in PHP.
This tag can be used outside of The Loop.
Note: This function outputs the complete shortlink for the post, to return the shortlink use wp_get_shortlink().
<?php the_shortlink($text, $title, $before, $after); ?>
$text
String • Default: 'This is the short link.'
Optional. Link text to display.
$title
String • Default: post title
Optional. Tool-tip text displayed with the link.
$before
String • Default: none
Optional. Text or HTML prefix added to the link.
$after
String • Default: none
Optional. Text or HTML suffix added to the link.
The Tagsthe_tags();
This template tag displays a link to the tag or tags a post belongs to. If no tags are associated with the current entry, nothing is displayed. This tag should be used within The Loop.
<?php the_shortlink($before, $sep, $after); ?>
$before
String • Default: 'Tags:'
Optional. Text to display before the actual tags are displayed.
$sep
String • Default: comma(,)
Optional. Text or character to display between each tag link.
$after
String • Default: ''
Optional. Text to display after the last tag.
The Titlethe_title();
Displays or returns the title of the current post. This tag must be within The Loop. If the post is protected or private, this will be noted by the words "Protected: " or "Private: " prepended to the title.
<?php the_title($before, $after, $echo); ?>
$before
String • Default: ''
Optional. Text to place before the title.
$after
String • Default: ''
Optional. Text to place after the title.
$echo
Boolean • Default: true
Optional. Display the title (true) or return it for use in php (false).
The Title Attributethe_title_attribute();
Displays or returns the title of the current post. It somewhat duplicates the functionality of the_title(), but provides a 'clean' version of the title for use in HTML attributes by stripping HTML tags and converting certain characters (including quotes) to their character entity equivalent; it also uses query-string style parameters. This tag must be within The Loop.
<?php the_title_attribute($before, $after, $echo); ?>
$before
String • Default: ''
Optional. Text to place before the title.
$after
String • Default: ''
Optional. Text to place after the title.
$echo
Boolean • Default: true
Optional. Echo the title (true) or return it for use in PHP (false).
The Title RSSthe_title_rss();
Display the post title in the feed.
<?php the_title_rss(); ?>
No parameters.
WP Link Pageswp_link_pages();
Displays page-links for paginated posts (i.e. includes the <!--nextpage--> Quicktag one or more times). Arguments are passed in as either an array or a query string. This tag must be within The Loop.
$args = array( | ||||
'before' | => | '<p>' . __('Pages:'), | ||
'after' | => | '</p>', | ||
'link_before' | => | '', | ||
'link_after' | => | '', | ||
'next_or_number' | => | 'number', | ||
'nextpagelink' | => | __('Next page'), | ||
'previouspagelink' | => | __('Previous page'), | ||
'pagelink' | => | '%', | ||
'echo' | => | true | ||
); |
<?php wp_link_pages($args); ?>
before
String • Default: '<p>Pages:'
Optional. Text to put before all the links.
after
String • Default: '</p>'
Optional. Text to put after all the links.
link_before
String • Default: ''
Optional. Text that goes before the text of the link.
link_after
String • Default: ''
Optional. Text that goes after the text of the link.
next_or_number
String • Default: 'number'
Optional. Indicates whether page numbers should be used. Valid values are: 'number', 'next'.
nextpagelink
String • Default: 'Next page'
Optional. Text for link to next page.
previouspagelink
String • Default: 'Previous page'
Optional. Text for link to previous page.
pagelink
String • Default: '%'
Optional. Format string for page numbers. % in the string will be replaced with the number, so Page % would generate "Page 1", "Page 2", etc.
echo
Boolean • Default: true
Optional. Echo the title (true) or return it for use in php (false).
Retrieve Post Titleget_the_title();
If the post is protected and the visitor is not an admin, then “Protected” will be displayed before the post title. If the post is private, then “Private” will be located before the post title.
<?php get_the_title($post); ?>
$post
Integer • Default: Global $post
(Optional) Post ID or WP_Post object.