In the sixth part of the series, we went through the fifth batch of WordPress template tags. In this seventh part, we're going to go through the sixth batch of the almost 200 template tags. Throughout the tutorial, we'll see template tags about authors and attachments.
Getting the URL of the Author's Post Archives: get_author_posts_url()
This template tag returns a link to an author's post archives.
Parameters
This template tag accepts two parameters:
-
$author_ID
(required—integer):
The ID of the author.
(Default: Current author) -
$author_nicename
(optional—string):
The "nice name" of the author. (A "nice name" is the sanitized version of "username" which can be used in URLs and such.)
(Default: Empty)
Usage
<?php // Getting and displaying current author's archives link. echo '<a href="' . get_author_posts_url() . '">' . __( 'Author\'s Archives', 'translation-domain' ) . '</a>'; ?>
Displaying a Link to the Author's Post Archives: the_author_posts_link()
This template tag outputs a link to the author's post archives.
Parameters
This template tag doesn't accept any parameters.
Usage
<?php the_author_posts_link(); ?>
Getting & Displaying the Author's Website Link: get_the_author_link()
& the_author_link()
These template tags return or echo a link to the author's website.
Parameters
These template tags don't accept any parameters.
Usage
<?php // Echoes the author link. the_author_link(); // Returns the author link. $the_author_link = get_the_author_link(); ?>
Getting & Displaying the Author's Meta Data: get_the_author_meta()
& the_author_meta()
These template tags fetch and display the meta data of an author.
Parameters
Both template tags accept two parameters:
-
$field
(required—string):
The field to fetch ('login', 'nicename', 'email', 'url', 'registered', or 'status').
(Default: Empty) -
$user_ID
(optional—integer):
The ID of the user.
(Default: Global$authordata
)
Usage
<?php // Get current author's registration date. get_the_author_meta( 'registered' ); // Display a specific author's email address. the_author_meta( 'email', 9 ); ?>
Getting & Displaying the Post Count of the Author: get_the_author_posts()
& the_author_posts()
These template tags count how many posts an author has written, and returns or displays the result.
Parameters
These template tags don't accept any parameters.
Usage
<?php the_author_posts(); ?>
Getting & Displaying the List of Authors: wp_list_authors()
This template tag returns or displays a list of authors writing on your website.
Parameters
This template tag accepts only one parameter:
-
$args
(optional—array):
An array of the following arguments:-
'orderby'
(string): How to order the links. Accepts 'user_nicename', 'user_email', 'user_url', 'user_registered', 'name', 'display_name', 'post_count', 'ID', 'meta_value', and 'user_login'.
(Default: 'name') -
'order'
(string): Whether to order items in ascending ('ASC') or descending ('DESC') order.
(Default: 'ASC') -
'number'
(integer): Number of authors to retrieve.
(Default:NULL
which means no limit) -
'option_count'
(boolean): Whether to display post count next to author names or not.
(Default:FALSE
) -
'exclude_admin'
(boolean): Whether to exclude the 'admin' (not all Administrators, just the one with the 'admin' username) from the list.
(Default:TRUE
) -
'show_fullname'
(boolean): Whether to show first and last names (TRUE
) or "display names" (FALSE
).
(Default:FALSE
) -
'hide_empty'
(boolean): Hide authors with no posts or not.
(Default:TRUE
) -
'echo'
(boolean): Whether to echo the output (TRUE
) or return it (FALSE
).
(Default:TRUE
) -
'feed'
(string): If set to any non-blank value, author feed links will appear next to author links. The value will be the anchor text of the links.
(Default: Empty) -
'feed_type'
(string): Type of feed ('rss', 'rss2', 'atom' or 'rdf').
(Default: Empty) -
'feed_image'
(string): Image path to override anchor text and show an image instead.
(Default: Empty) -
'style'
(string): Style to display authors—'list' to generate an unordered list or 'none' to generate a list of links separated with commas.
(Default: 'list') -
'html'
(boolean): Whether to generate HTML or plain text.
(Default:TRUE
) -
'include'
(string): Comma-separated list of IDs to include.
(Default: Empty) -
'exclude'
(string): Comma-separated list of IDs to exclude.
(Default: Empty)
-
Usage
<?php $args = array( // Order by registration date. 'orderby' => 'user_registered', // Display post counts next to the names. 'option_count' => true, // Show full names. 'show_fullname' => true, // Show authors who doesn't have any posts. 'hide_empty' => false, // Exclude Mike, he has betrayed us! 'exclude' => 31 ); wp_list_authors( $args ); ?>
Getting & Displaying an HTML Dropdown of Users: wp_dropdown_users()
This template tag returns or echoes a dropdown menu (<select>
) of users.
Parameters
This template tag accepts only one parameter:
-
$args
(optional—array):
An array of the following arguments:-
'show_option_all'
(string): Text to display as the dropdown default.
(Default: Empty) -
'show_option_none'
(string): Text to display as the dropdown default when no users were found.
(Default: Empty) -
'option_none_value'
(integer or string): Value to use for 'show_option_none' when no users were found.
(Default: -1) -
'hide_if_only_one_author'
(string): Whether to hide the dropdown if there's only one author.
(Default: Empty) -
'orderby'
(string): Which column to use for ordering users.
(Default: 'display_name') -
'order'
(string): Which direction to order users.
(Default: 'ASC') -
'include'
(array or string): An array or a comma-separated list of user IDs to show (and exclude the rest).
(Default: Empty) -
'exclude'
(array or string): An array or a comma-separated list of user IDs to exclude from the list.
(Default: Empty) -
'multi'
(boolean or integer): Whether to skip the ID of theSELECT
element. (Useful if you're going to display more than one users dropdown.)
(Default: 0) -
'show'
(string): What to show as list item names.
(Default: 'display_name') -
'echo'
(boolean or integer): Whether to display or retrieve content.
(Default: 1) -
'selected'
(integer): The user ID to be selected by default.
(Default: 0) -
'include_selected'
(boolean): Whether to always include the selected user ID in the dropdown..
(Default:FALSE
) -
'name'
(string): TheNAME
attribute value for theSELECT
element.
(Default: 'user') -
'id'
(string): The ID for theSELECT
element.
(Default: Empty) -
'class'
(string): CSS class for theSELECT
element.
(Default: Empty) -
'blog_id'
(optional; integer): ID of the blog to retrieve users.
(Default: Current blog) -
'who'
(string): Which type of users to query (Accepts 'authors' or an empty string).
(Default: Emtpy)
-
Usage
<?php $args = array( // Order by registration date. 'orderby' => 'user_registered', // Exclude admin. 'exclude' => 1, // I'm going to use more than one users dropdown, so don't put a unique id. 'multi' => 1, // Don't echo, just return the output. 'echo' => 0, // NAME parameter of the SELECT tag. 'name' => 'brands', // Get only authors. 'who' => 'authors' ); $brands_dropdown = wp_categories_dropdown( $args ); ?>
Getting & Displaying the Featured Image of the Post: get_the_post_thumbnail()
& the_post_thumbnail()
Well, the title explains it all.
Parameters
get_the_post_thumbnail()
accepts three parameters:
-
$post_ID
(optional—integer):
The ID of the post.
(Default: Current post) -
$size
(optional—string):
Size of the image.
(Default: 'post-thumbnail') -
$attr
(optional—string or array):
An array of the following attributes:- 'src': The image source
- 'class': The CSS class (or classes, separated with spaces)
- 'alt': The alt text
And the_post_thumbnail()
accepts two parameters:
-
$size
(optional—string):
Size of the image.
(Default: 'post-thumbnail') -
$attr
(optional—string or array):
An array of the following attributes:- 'src': The image source
- 'class': The CSS class (or classes, separated with spaces)
- 'alt': The alt text
Usage
<?php // Get the post's featured image with default options. get_the_post_thumbnail(); // Display the post's featured image with custom options. $attr = array( 'class' => 'post-featured-image', 'alt' => __( 'Featured Image', 'translation-domain' ) ); the_post_thumbnail( 'medium', $attr ); ?>
Getting & Displaying the Permalink of an Attachment Page: wp_get_attachment_link()
& the_attachment_link()
These template tags return or display the permalink of the attachment, so it can be opened in another page.
Parameters
wp_get_attachment_link()
accepts five parameters:
-
$post_ID
(optional—integer or object):
The ID of the post.
(Default: Current post) -
$size
(optional —string):
Size of the image.
(Default: 'thumbnail') -
$permalink
(optional—boolean):
Whether to include permalink or not.
(Default:FALSE
) -
$icon
(optional—boolean):
Whether to include icon.
(Default:FALSE
) -
$text
(optional—string or boolean):
Text to display for the link.
(Default:FALSE
)
And the_attachment_link()
accepts four parameters:
-
$post_ID
(optional—integer or object):
The ID of the post.
(Default: Current post) -
$fullsize
(optional—boolean):
Whether to use full sized image or not.
(Default:FALSE
) -
$deprecated
(deprecated—boolean):
This parameter is deprecated and you should just pass it asTRUE
orFALSE
(doesn't matter which). -
$permalink
(optional—boolean):
Whether to include permalink or not.
(Default:FALSE
)
Usage
<?php // Get current attachment's link with a large image. wp_get_attachment_link( $post->ID, 'large' ); // Display current attachment's link. the_attachment_link(); ?>
Getting the ID of the Post's Featured Image: get_post_thumbnail_id()
This template tag gets the ID of a post's featured image.
Parameters
This template tag accepts only one parameter:
-
$post_ID
(optional—integer):
The post's ID to retrieve the thumbnail ID from.
(Default: Current post)
Usage
<?php // Get current post's featured image's id. $featured_image_ID = get_post_thumbnail_id(); // Rcho a specific post's featured image's id. echo get_post_thumbnail_id( 39 ); ?>
Getting the Attachment Image: wp_get_attachment_image()
This template tag returns a given attachment image's HTML code (<img />
).
Parameters
This template tag accepts four parameters:
-
$attachment_ID
(optional—integer):
The ID of the attachment.
(Default: Current attachment) -
$size
(optional—string):
The image size.
(Default: 'thumbnail') -
$icon
(optional—boolean):
Whether to include icon or not.
(Default:FALSE
) -
$attr
(optional—string or array):
An array of the following attributes:- 'src': The image source
- 'class': The CSS class (or classes, separated with spaces)
- 'alt': The alt text
Usage
<?php $attr = array( 'class' => 'mytheme-attachment', ); echo wp_get_attachment_image( $post->ID, 'thumbnail', false, $attr ); ?>
Getting the Attachment Image URL: wp_get_attachment_image_src()
This template tag returns the URL of an attachment image of your choice.
Parameters
This template tag accepts three parameters:
-
$attachment_ID
(optional—integer):
The ID of the attachment.
(Default: Current attachment) -
$size
(optional—string):
The image size.
(Default: 'thumbnail') -
$icon
(optional—boolean):
Whether to include icon or not.
(Default:FALSE
)
Usage
<?php // Get a specific attachment's url. $special_attachment_url = wp_get_attachment_image_src( 199 ); ?>
Getting the Attachment Image Meta Data: wp_get_attachment_metadata()
This template tag returns the meta data of a given attachment.
Parameters
This template tag accepts two parameters:
-
$attachment_ID
(optional—integer):
The ID of the attachment.
(Default: 0) -
$unfiltered
(optional—boolean):
If this is set toTRUE
, thewp_get_attachment_metadata
filter will not be run.
(Default:FALSE
)
Usage
<?php print_r( wp_get_attachment_metadata( $post->ID ) ); ?>
Getting the Next Image of the Same Post: next_image_link()
This template tag returns the "next image" link for image attachment pages.
Parameters
This template tag accepts two parameters:
-
$size
(optional—string):
Size of the image.
(Default: 'thumbnail') -
$text
(optional—string or boolean):
Text to display for the link.
(Default:FALSE
)
Usage
<?php echo next_image_link( 'medium', __( 'Next Image', 'translation-domain' ) ); ?>
Getting the Previous Image of the Same Post: previous_image_link()
This template tag returns the "previous image" link for image attachment pages.
Parameters
This template tag accepts two parameters:
-
$size
(optional—string):
Size of the image.
(Default: 'thumbnail') -
$text
(optional—string or boolean):
Text to display for the link.
(Default:FALSE
)
Usage
<?php echo previous_image_link( 'medium', __( 'Previous Image', 'translation-domain' ) ); ?>
Conclusion
Hope you liked this sixth batch of template tags. There are two 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