• Hi people

    I got one post that have 20 images. Every of the images are link to attachment file and now in the attachment file template (attachment.php) I want to show not only one main photo that is attached but under I need to display all images from the one post I mentioned. Something like gallery under main photo with tumbnails. I was looking for some solutions but I feel I need a help. I`m not a programist so I need ready solutions that I can adapt to my case.

    Thanks for any help
    Rafal

    • This topic was modified 5 years, 4 months ago by zecke.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter zecke

    (@zecke)

    I think I got something. It`s working but showing only one thumbnail. How to change the code to show them all ?

    https://www.uforocks.com/blog/get-images-attached-to-post

    • This reply was modified 5 years, 4 months ago by zecke.
    Moderator bcworkz

    (@bcworkz)

    $arrImages has all of the image attachment objects in an array, but the provided code only uses the first image. You need a foreach loop to process every attachment. wp_get_attachment_image() will return a complete <img> tag given an attachment ID from the current object in the loop.

    get_permalink() in the proffered code will just link to the current page. If you want to link to other attachment pages use get_attachment_link().

    Thread Starter zecke

    (@zecke)

    Thank You bcworkz. When I’m reading Your post I know that You know what Youre talking about. But as I said I'm not a programmer, could You be so kind and change the code for my needs ? And if I'm asking too much please tell me. Im writing to You from Poland – wild country in center of Europe. I can do some wild stuff if You want πŸ˜‰

    Moderator bcworkz

    (@bcworkz)

    DzieΕ„ dobry. I know Poland. Beautiful country. I spent some time there visiting some Polish friends. I don’t need anything wild, but thanks for the offer. I think you could have pulled this together on your own if you had really tried. Maybe it wouldn’t be perfect and you’d still need help, but you will more likely get help if you’re already making your own best effort. But since you asked nicely, here you are:

    function bdw_get_images() {
     
        // Get images for this post
        $arrImages = get_children('post_type=attachment&post_mime_type=image&post_parent=' . get_the_ID() );
     
        // If images exist for this page
        if($arrImages) {
     		foreach ( $arrImages as $img ) {
     		
    		    // Build the <img> string
    		    $sImgString = '<a href="' . get_attachment_link( $img->ID ) . '" class="bdw-img-link">' .
    		                        wp_get_attachment_image( $img->ID, 'thumbnail') .
    		                    '</a>';
    	 
    		    // Print the image
    		    echo $sImgString;
            } //end foreach
        } //end if
    }

    You can specify other image sizes besides “thumbnail” in wp_get_attachment_image(). The actual width displayed depends on your site’s CSS. You might need to add something like this to Additional CSS in the customizer:

    a.bdw-img-link {
       width: 150px;
       float: left;
       margin: 20px;
    }

    Coordinate the width value with the image size used.

    Thread Starter zecke

    (@zecke)

    DzieΕ„ dobry πŸ™‚

    Thank You. Before Your help I tried to change some things with my famous method: “trials and errors” but all I got was errors…

    Im happy that You visited Poland and whats more that You enjoyed Your stay !

    I pasted Your code and I got nothing. Like I said before I call the function on attachment.php page. Any ideas ?

    Moderator bcworkz

    (@bcworkz)

    Hmmm… I think get_the_ID() is inappropriate, my bad. Please replace with this: $post->post_parent
    Because we need the ID of the current attachment’s parent post. Attachments don’t have children themselves.

    Also place global $post; as the first statement of the function.

    Hey, “trial and error” is my favorite method too! πŸ™‚

    Thread Starter zecke

    (@zecke)

    Wow. Thank You ! Its working like a charm !

    Bcworkz please tell me if I have any problems with modyfing php scripts for wordpress (twice a year I think) can I contact You ? And how to do this ? And next time I can offer not only doing some wild stuff πŸ˜‰ but I can just pay you through paypal.

    Are You from US ?

    Thanks again

    Moderator bcworkz

    (@bcworkz)

    Thank you for the kind offer, but offering payment or any sort of compensation is not allowed in these forums. In any case, I’m not available for off-forum work. You can always ask questions in these forums. I or someone will try to answer, but don’t expect much in the way of custom coded solutions unless it’s fairly simple. It usually works better if you post a code snippet of your best effort and ask what’s wrong or needed to get it to work. I know you’re not a programmer, but it seems you have some modest coding skills.

    You shouldn’t ever modify existing code. There’s normally some way through hooks or other means to modify behavior by adding your own code. All of your custom code can reside in a child theme or custom plugin. There you may modify or add as much as you like.

    Thread Starter zecke

    (@zecke)

    Ok. I get it. Thanks again. I hope we will meet on this forum again

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Show all other attachment images on attachment template’ is closed to new replies.