• Resolved kolshix

    (@kolshix)


    There are navigation buttons back and forth, implemented through ?<?php echo previous_image_link()?

    QUESTION:
    how to add 2 more buttons on the very first embedded picture of the current picture of the current and post to the most recent picture?

    
    I now have navigation ?Image Attachment Template (image.php)? looks like this:
      <  3/10  > 
    [-----------]
    [--picture--]
    [-----------]
    
    I want to be like this
    
    <<  <  3/10  >  >>
    [----------------]
    [-----picture----]
    [----------------]
    

    It is advisable to immediately codes with notes, I will be grateful for any information.
    Plugins are not accepted.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    You’d best see how adjacent_image_link() source works, it has direct bearing on what you need to do. This is the function used by both previous_image_link() and next_image_link().

    You’ll see a call is made to get_children() and the current image is located within all the children in order to determine what is next and previous. It’s actually rather inefficient to repeatedly query for children for each nav item when all items could be determined from the same call. I’d suggest you make your own template tag function based on adjacent_image_link() source code that outputs the entire “<< < 3/10 > >>” nav block all at once based on a single get_children() call. The results of that one query is all you need to output all of the nav items.

    Since all the children are in $attachments, the first image object will be $attachments[0] and the last will be $attachments[ count( $attachments ) – 1 ]. The IDs of each is one of the object properties, which can be used in wp_get_attachment_link().

    • This reply was modified 8 years, 2 months ago by bcworkz.
    Thread Starter kolshix

    (@kolshix)

    [Resolved][Resolved][Resolved][Resolved][Resolved][Resolved]
    Many thanks!
    I really, really do not quite understand the code WordPress! But your links helped.
    I made as i could:

    
    <?php 
    	      
    	        $attachments = array_values( get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) ) );
    	
          		$attachment_id = $attachments[0]->ID;
    	        $output = get_attachment_link( $attachment_id, $size, true, false, $text );
    	    
    	 	echo '<a href="' . $output . '">First Image</a>';
    		
    		$last_img = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_parent = ' $post->post_parent' AND post_type = 'attachment'" ) .''; 
    		$attachment_id = $attachments[$last_img - 1]->ID;
    	        $output = get_attachment_link( $attachment_id, $size, true, false, $text );
    	    
    	 	echo ' ||| <a href="' . $output . '">Last image</a>';
    ?>
    

    It was only a template, then completed yet as I like.
    To unite all the buttons (first, previous, next and last) one code by type (adjacent_image_link()) and add CSS.

    • This reply was modified 8 years, 2 months ago by kolshix.
    • This reply was modified 8 years, 2 months ago by kolshix.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘how to get link to first and last image?’ is closed to new replies.