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
<?php // Add an extra class and get post classes. $post_classes = get_post_class( 'mytheme-post-class' ); // Display the post classes of a specific post. post_class( '', 42 ); ?>
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
<?php // Get the permalink of the current post. get_permalink(); // Display the permalink of a specific post. the_permalink( 69 ); ?>
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
<?php the_ID(); ?>
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
<?php // Display the post title with a prefix. single_post_title( __( 'Blog Post', 'translation-domain' ) . ': ' ); // Get the post title. $post_title = single_post_title( '', false ); ?>
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 »') -
$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
<?php // Get the default next post link. $next_post_link = get_next_post_link(); /** * Display the next post link with some adjustments * (different link format, different anchor text and * next post should be in the same category with current post). */ next_post_link( __( 'Next Post', 'translation-domain' ) . ': %link', '<i class="icon-post"></i>%title', true ); ?>
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 »') -
$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
<?php // Get the default previous post link. $previous_post_link = get_previous_post_link(); /** * Display the previous post link with some adjustments * (different link format, different anchor text and * previous post should be in the same category with current post). */ previous_post_link( __( 'Previous Post', 'translation-domain' ) . ': %link', '<i class="icon-post"></i>%title', true ); ?>
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
<?php // Get and print a specific page's link. echo '<a href="' . get_page_link( 9 ) . '">' . __( 'About', 'translation-domain' ) . '</a>'; ?>
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
<?php // Get a specific post's permalink. $special_post_permalink = get_post_permalink( 15 ); // Display current post's permalink. post_permalink(); ?>
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
<?php // Get the post shortlink with default options. wp_get_shortlink(); // Display the post shortlink with customized parameters. the_shortlink( __( 'Shortlink', 'translation-domain' ), '', '<i class="icon-link"></i>' ); ?>
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
<?php // Retrieve the current post's "edit post" link. $edit_post_link = get_edit_post_link(); // Retrieve a specific post's "edit post" link. $edit_post_link = get_edit_post_link( 157 ); // Display the current post's "edit post" link. edit_post_link( __( 'Edit', 'translation-domain' ), '', '<i class="icon-edit"></i>' ); // Display a specific post's "edit post" link. edit_post_link( __( 'Edit', 'translation-domain' ), '', '', 157 ); ?>
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
<?php // Get and echo the "trash" link of the current post. echo get_delete_post_link(); // Get and echo the "force delete" link of the current post. echo get_delete_post_link( $post->ID, '', true ); ?>
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 theid
parameter of the<a>
tag to the post title ('title') or post ID ('id' or any other text).
(Default: 'id')
Usage
<?php // Display a permalink anchor with its title. permalink_anchor( 'title' ); // It outputs as follows: // <a id="my-post-title"></a> // Display a permalink anchor with its id. permalink_anchor(); permalink_anchor( 'id' ); // Both functions output as follows: // <a id="post-1234"></a> ?>
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 loadheader.php
)
Usage
<?php // Load header.php. get_header(); // Load header-special.php. get_header( 'special' ); ?>
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 loadfooter.php
)
Usage
<?php // Load footer.php. get_footer(); // Load footer-special.php. get_footer( 'special' ); ?>
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 loadsidebar.php
)
Usage
<?php // Load sidebar.php. get_sidebar(); // Load sidebar-special.php. get_sidebar( 'special' ); ?>
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
<?php // Load shortcodes.php. get_template_part( 'shortcodes' ); // Load a content part dynamically (e.g. content-1.php, content-2.php or content-3.php). $number = some_function(); get_template_part( 'content', $number ); ?>
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!
Comments