2

This question already has an answer here:

How to make Next and Previous attached image navigation on the attachment page?

' . __( 'Previous Image', '$text_domain' ) . '' ); ?> ' . __( 'Next Image', '$text_domain' ) . '' ); ?>

working great :)

marked as duplicate by Pieter Goosen, TheDeadMedic, Nicolai, Johannes Pille, fuxia Jul 18 '14 at 16:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Why is this marked a duplicate? The answer from the other topic is not using wp_get_attachment_link and it's only searching attachments from the parent post. – keesiemeijer Jul 18 '14 at 19:25
4

Use previous_image_link and next_image_link in a image.php or attachment.php file.

<nav id="image-navigation" class="navigation image-navigation">
                <div class="nav-links">
                <?php previous_image_link( false, '<div class="previous-image">' . __( 'Previous Image', '$text_domain' ) . '</div>' ); ?>
                <?php next_image_link( false, '<div class="next-image">' . __( 'Next Image', '$text_domain' ) . '</div>' ); ?>
                </div><!-- .nav-links -->
            </nav><!-- #image-navigation -->

Source: Twenty Fourteen

0

Use a date query to get the next and previous (attachment) post by date. And exclude all child attachments from the current parent post.

<?php
// use in template for single attachment display

// get all children ids (to exclude from the queries for next and previous post)
$args = array(
    'post_status'    => 'inherit',
    'post_type'      => 'attachment',
    'post_mime_type' => 'image',
    'fields'         => 'ids',
    'post_parent'    => $post->post_parent,
);

// get image attachments of parent (current post  will be included)
$children_ids = get_children( $args );

$args =  array(
    'post_status'    => 'inherit',
    'post_type'      => 'attachment',
    'post_mime_type' => 'image',
    'posts_per_page' => 1,
    'order'          => 'DESC',
    'post__not_in'   => $children_ids,
    'date_query'     => array(
        array(
            'before' => $post->post_date,
            'column' => 'post_date',
        ),
    )
);

// get attachment with post date before current post date
$previous = get_posts( $args );

if ( isset( $previous[0]->ID ) ) {
    echo wp_get_attachment_link( $previous[0]->ID, 'thumbnail', true );
}

// remove before
unset( $args['date_query'][0]['before'] );

// add after and change order
$args['order'] = 'ASC';
$args['date_query'][0]['after'] = $post->post_date;

// get attachment with post date after current post date
$next = get_posts( $args );

if ( isset( $next[0]->ID ) ) {
    echo wp_get_attachment_link(  $next[0]->ID, 'thumbnail', true );
}
?>

Not the answer you're looking for? Browse other questions tagged or ask your own question.