WordPress previous_image_link() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
Prints a link

Definition

In WordPress, previous_image_link() prints a link to the previous image attached to the same post. Use it on an image attachment page so visitors can go back through a gallery.

You can show a thumbnail (default) or your own text.

Syntax

A few ways to call previous_image_link().

WordPress
<?php
previous_image_link( false, '&laquo; Previous image' );
CallWhat it does
previous_image_link()Link shown as a thumbnail of the previous image.
previous_image_link( 'medium' )Link shown as a medium-size image.
previous_image_link( false, 'Previous' )Link shown as text.
next_image_link()The same, for the next image.

It prints the link. If there is no previous image, nothing is printed.

Full declaration
WordPress
previous_image_link( string|int[] $size = 'thumbnail', string|false $text = false ): void

Parameters

ParameterDescription
$sizestring|int[]. The image size for the thumbnail link. Default is thumbnail.
$textstring|false. Link text. If false, the image itself is used. Default is false.

Return Value

Return ValueDescription
voidNothing is returned. The link is printed.

WordPress Examples

Three short examples. Each one shows the code and what it prints.

Example 1 — Show a text link

Print a simple previous image link.

WordPress
<div class="image-nav">
  <?php previous_image_link( false, '&laquo; Previous image' ); ?>
</div>

How It Works

  1. When you give link text, the image size is not used.
  2. The text becomes the clickable link.
  3. The link goes to the previous attachment page.

Example 2 — Show a thumbnail link

Leave the text out to show the previous image as a thumbnail.

WordPress
<?php previous_image_link( 'thumbnail' ); ?>

How It Works

  1. With no text, the image is shown in the size you pass.
  2. The thumbnail is wrapped in a link.
  3. Try medium for a bigger preview.

Example 3 — Show previous and next

Put both links in one block.

WordPress
<nav class="image-nav">
  <?php previous_image_link( false, '&laquo; Previous' ); ?>
  <?php next_image_link( false, 'Next &raquo;' ); ?>
</nav>

How It Works

  1. Both functions work from the current image.
  2. Each prints nothing if there is no image in that direction.
  3. This gives readers simple gallery navigation.

Frequently Asked Questions

It prints a link to the previous image attached to the same post, from an image attachment page.
In an attachment template such as image.php or attachment.php.
Yes. Pass false as the size and your text as the second argument.
Nothing is printed.
next_image_link() prints the link to the next image.

Browse all post functions

Open the functions index for simple guides with examples and FAQs.

Post Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful