The Tuts+ Guide to Template Tags: Fifth Batch

In the fifth part of the series, we went through the fourth batch of WordPress template tags. In this sixth part, we're going to go through the fifth batch of the almost 200 template tags. Throughout the tutorial, we'll see template tags about posts, pages and template parts.

Getting & Displaying Automatic Classes for Each Post: get_post_class() & post_class()

These template tags let you add extra classes to use in each post's container.

Parameters

Both template tags accept two parameters:

  • $class (optional—string):
    Extra CSS class names to add.
    (Default: Empty)
  • $post_ID (optional—integer or object):
    The ID of the post.
    (Default: Current post)

Usage

Getting & Displaying the Permanent Link of Current Post: get_permalink() & the_permalink()

This template tag returns or displays a "permalink" of the post.

Parameters

the_permalink() doesn't accept any parameters, but get_permalink() accepts two parameters:

  • $post_ID (optional—integer or object):
    The ID of the post.
    (Default: Current post)
  • $leavename (optional—boolean):
    Whether to keep page name or not.
    (Default: FALSE)

Usage

Getting & Displaying the Post ID: get_the_ID() & the_ID()

These template tags return and display the post ID.

Parameters

These template tags don't accept any parameters.

Usage

Getting & Displaying a Page Title for the Post: single_post_title()

This template tag returns and displays the page's title for using in page titles (the titles that go inside the <title> tag).

Parameters

This template tag accepts two parameters:

  • $prefix (optional—string):
    Prefix to the title.
    (Default: Empty)
  • $echo (optional—boolean):
    Whether echo (TRUE) or return (FALSE) the tag.
    (Default: TRUE)

Usage

Getting & Displaying the Link of the Next Post: get_next_post_link() & next_post_link()

These template tags let you return or display a "next post" link for, say, your single post pages.

Parameters

Both template tags accept five parameters:

  • $format (optional—string):
    Format of the link where %link is the $link parameter.
    (Default: '%link &raquo;')
  • $link (optional—string):
    Format of the anchor text where %title is the post title.
    (Default: '%title')
  • $in_same_term (optional—boolean):
    Whether to make the link a post from the same taxonomy term.
    (Default: FALSE)
  • $excluded_terms (optional—string or array):
    Excluded terms list.
    (Default: Empty)
  • $taxonomy (optional—string):
    Taxonomy to filter the next post.
    (Default: 'category')

Usage

Getting & Displaying the Link of the Previous Post: get_previous_post_link() & previous_post_link()

These template tags let you return or display a "previous post" link for, say, your single post pages.

Parameters

Both template tags accept five parameters:

  • $format (optional—string):
    Format of the link's anchor text.
    (Default: '%link &raquo;')
  • $link (optional—string):
    Format of the permalink.
    (Default: '%title')
  • $in_same_term (optional—boolean):
    Whether to make the link a post from the same taxonomy term.
    (Default: FALSE)
  • $excluded_terms (optional—string or array):
    Excluded terms list.
    (Default: Empty)
  • $taxonomy (optional—string):
    Taxonomy to filter the previous post.
    (Default: 'category')

Usage

Getting the Permalink of a Page: get_page_link()

This template tag returns the permalink URL of a given page.

Parameters

This template tag accepts three parameters:

  • $post_ID (optional—integer or object):
    The ID of the post.
    (Default: Current post)
  • $leavename (optional—boolean):
    Whether to keep page name or not.
    (Default: FALSE)
  • $sample (optional—boolean):
    Whether is it a sample permalink.
    (Default: FALSE)

Usage

Getting & Displaying the Permalink of a Post: get_post_permalink() & post_permalink()

These template tags return and display the permalink of the given post.

Parameters

get_post_permalink() accepts three parameters:

  • $post_ID (optional—integer):
    The ID of the post.
    (Default: Current post)
  • $leavename (optional—boolean):
    Whether to keep page name or not.
    (Default: FALSE)
  • $sample (optional—boolean):
    Whether is it a sample permalink.
    (Default: FALSE)

And post_permalink() accepts only one parameter:

  • $post_ID (optional—integer or object):
    The ID of the post.
    (Default: Current post)

Usage

Getting & Displaying the Shortlink of the Post: wp_get_shortlink() & the_shortlink()

These template tags return or display a shortlink for your posts.

Parameters

wp_get_shortlink() accepts three parameters:

  • $ID (optional—integer):
    Post or blog ID.
    (Default: Current post or blog)
  • $context (optional—string):
    Whether the ID is a blog ID, a post ID or a media ID. You can set it to 'post' (for blog posts) or 'query' (for every other kind).
    (Default: 'post')
  • $allow_slugs (optional—boolean):
    Whether to allow slugs in the shortlink.
    (Default: TRUE)

And the_shortlink() template tag accepts four parameters:

  • $text (optional—string):
    Text to display for the link.
    (Default: "This is the short link")
  • $title (optional—string):
    Tooltip title for the link.
    (Default: Empty, which is converted to sanitized post title)
  • $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)

Usage

Getting & Displaying the "Edit Post" Link: get_edit_post_link() & edit_post_link()

These template tags let you get or echo a "edit this post" link to use in your templates.

Parameters

get_edit_post_link() accepts two parameters:

  • $post_ID (optional—integer):
    The ID of the post.
    (Default: Current post)
  • $context (optional—string):
    Whether to escape the URL or not. Use anything but 'display' to prevent encoding the link.
    (Default: 'display')

And edit_post_link() accepts four parameters:

  • $text (optional—string):
    Text to display for the link.
    (Default: 'Edit This')
  • $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)
  • $post_ID (optional—integer):
    The ID of the post.
    (Default: Current post)

Usage

Getting the "Delete Post" Link: get_delete_post_link()

This template tag lets you use a "delete this post" link in your templates.

Parameters

This template tag accepts three parameters:

  • $post_ID (optional—integer):
    The ID of the post.
    (Default: Current post's ID.)
  • $deprecated (deprecated—string):
    This parameter is deprecated, pass it empty.
    (Default: Empty)
  • $force_delete (optional—boolean):
    Whether to delete completely, without sending the comment to Trash.
    (Default: FALSE)

Usage

Displaying Permalink "Anchor" for the Current Post: permalink_anchor()

This template tag displays a permalink anchor ID for a post. Check the example for more explanation.

Parameters

This template tag accepts only one parameter:

  • $mode (optional—string):
    Whether to set the id parameter of the <a> tag to the post title ('title') or post ID ('id' or any other text).
    (Default: 'id')

Usage

Loading Header Template: get_header()

This well-known template tag lets you load an external PHP file of a header.

Parameters

This template tag accepts only one parameter:

  • $name (optional—string):
    Name of the header file (header-{$name}.php).
    (Default: Empty, which will load header.php)

Usage

Loading Footer Template: get_footer()

This commonly used template tag lets you load an external PHP file of a footer.

Parameters

This template tag accepts only one parameter:

  • $name (optional—string):
    Name of the footer file (footer-{$name}.php).
    (Default: Empty, which will load footer.php)

Usage

Loading Sidebar Template: get_sidebar()

This popular template tag lets you load an external PHP file of a sidebar.

Parameters

This template tag accepts only one parameter:

  • $name (optional—string):
    Name of the sidebar file (sidebar-{$name}.php).
    (Default: Empty, which will load sidebar.php)

Usage

Loading a Template File: get_template_part()

This template tag lets you load an external PHP file in your theme folders.

Parameters

This template tag accepts two parameters:

  • $slug (required—string):
    The slug of the template filename.
    (Default: NULL)
  • $name (optional—string):
    The custom name that's suffixed to the filename with a hyphen.
    (Default: NULL)

Usage

Conclusion

Hope you liked this fifth batch of template tags. There are three 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!

Tags:

Comments

Related Articles