Quantcast
Channel: Ruben Sargsyan » WordPress
Viewing all articles
Browse latest Browse all 16

Get post next and previous attached images in WordPress

$
0
0

Here is an example of functions to get next and previous attached images of the post:

<?php
function get_post_next_attached_image( $attachment_id ) {
    global $wpdb;
 
    $attachment = get_post( $attachment_id );
 
    $next_attached_image = $wpdb->get_row("SELECT * FROM " . $wpdb->posts . " WHERE ID = (SELECT MIN(ID) FROM " . $wpdb->posts . " WHERE post_parent = '" . $attachment->post_parent . "' AND post_mime_type LIKE 'image%' AND ID > '" .$attachment_id . "' ORDER BY menu_order, ID);");
 
    return $next_attached_image;
}
 
function get_post_previous_attached_image( $attachment_id ) {
    global $wpdb;
 
    $attachment = get_post( $attachment_id );
 
    $previous_attached_image = $wpdb->get_row("SELECT * FROM " . $wpdb->posts . " WHERE ID = (SELECT MAX(ID) FROM " . $wpdb->posts . " WHERE post_parent = '" . $attachment->post_parent . "' AND post_mime_type LIKE 'image%' AND ID < '" .$attachment_id . "' ORDER BY menu_order, ID);");
 
    return $previous_attached_image;
}
?>

Viewing all articles
Browse latest Browse all 16

Trending Articles