Codex

Attention Help us to improve the Codex by filling out our documentation survey!

Function Reference/previous post link

Contents

Description

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.

Arguments

<?php previous_post_link($format, $link, $in_same_cat = false, $excluded_categories = ''); ?>

Parameters

format 
(string) 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 <a href=..." Put HTML tags here to style the final results. Defaults to '&laquo; %link'.
link 
(string) Link text to display. Defaults to previous post's title ('%title').
in_same_cat 
(boolean) 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. Options are:
  • TRUE
  • FALSE (Default)
excluded_categories 
(string) Numeric category ID(s) from which the previous post should not be listed. Separate multiple categories with and; example: '1 and 5 and 15'. There is no default.

Examples

Default Usage

Displays link with left angular quote («) followed by the post title of the previous post (chronological post date order).

« Previous Post Title
<?php previous_post_link(); ?>

Bold Post Title As Link

Displays link with previous chronological post's title wrapped in 'strong' tags (typically sets text to bold).

Previous Post Title

 <?php previous_post_link('<strong>%link</strong>'); ?> 

Text As Link, Without Post Title, Within Same Category

Displays custom text as link to the previous post within the same category as the current post. Post title is not included here. "Previous in category" is the custom text, which can be changed to fit your requirements.

Previous in category

 <?php previous_post_link('%link''Previous in category'TRUE); ?> 

Within Same Category, Excluding One

Displays link to previous post in the same category, as long as it is not in category 13 (the category ID #). You can change the number to any category you wish to exclude. Exclude multiple categories by using " and " as a delimiter.

Previous in category

 <?php previous_post_link('%link''Previous in category'TRUE'13'); ?> 

Notes

Resources

Source File

previous_post_link() is located in wp-includes/link-template.php.

Related

Pagination: posts_nav_link(), next_posts_link(), previous_posts_link(), get_next_posts_link(), get_previous_posts_link(), paginate_links() wp_link_pages(), next_post_link(), previous_post_link(), next_image_link(), previous_image_link(),

See also index of Function Reference and index of Template Tags.