get_previous_posts_link()WP 2.7.0

Retrieves the previous posts page link.

Hooks from the function

Return

String|null. HTML-formatted previous page link.

Usage

get_previous_posts_link( $label );
$label(string)
Previous page link text.
Default: null

Examples

0

#1 Let's write the link to the previous posts into a variable and display it

$prev = get_previous_posts_link();
echo $prev;
0

#2 Removing Trailing Slashes from Prev & Next links

If you are using no-end-trailing slashes for your URLs. You may add following filter in your functions file to remove trailing slashes from generated links from get_previous_posts_links() function.

add_filter( 'get_pagenum_link', 'user_trailingslashit' );

Notes

  • Global. Int. $paged

Changelog

Since 2.7.0 Introduced.

get_previous_posts_link() code WP 6.4.3

function get_previous_posts_link( $label = null ) {
	global $paged;

	if ( null === $label ) {
		$label = __( '« Previous Page' );
	}

	if ( ! is_single() && $paged > 1 ) {
		/**
		 * Filters the anchor tag attributes for the previous posts page link.
		 *
		 * @since 2.7.0
		 *
		 * @param string $attributes Attributes for the anchor tag.
		 */
		$attr = apply_filters( 'previous_posts_link_attributes', '' );

		return sprintf(
			'<a href="%1$s" %2$s>%3$s</a>',
			previous_posts( false ),
			$attr,
			preg_replace( '/&([^#])(?![a-z]{1,8};)/i', '&#038;$1', $label )
		);
	}
}