0

I am trying to make the inserted images from a post (single.php) to point to (single-attachement.php) an attachment page. So far I checked a post here on stackexchange and added this code on the top, before the header of single.php, just that it's showing me a link to the last image on the page, instead of modifying the structure of the image link.

if ( $attachments = get_children( array(
'post_type' => 'attachment',
'post_mime_type'=>'image',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID
)));
foreach ($attachments as $attachment) {
echo wp_get_attachment_link( $attachment->ID, '' , true, false, 'Link to image attachment' );
}
  • anyone, please help, really did not find how to get this done. – Adrian Oct 24 '14 at 12:11
1

If I understand you correctly, you need to link your images not to the file, but to the attachment template (attachment.php). If so, then try the following code in your single.php:

 <?php if( has_post_thumbnail() ) {
    $attachment_page_url = '';
    $attachment_page_url = get_attachment_link( get_post_thumbnail_id() ); ?>
    <a href="<?php echo $attachment_page_url; ?>" class="featured-image">
        <?php the_post_thumbnail(); ?>
    </a>
<?php } ?>

We are checking if there's any post_thumbnail (Featured Image), then if found getting the attachment link using the post_thumbnail_id, and passing that to the anchor tag of the featured image.

Hope that's clear. :)

EDIT

So you're affirmative that the bit of code you mentioned actually works, but it's fetching only one image (the last one only). So, I'm sticking with the same code with slight changes:

<?php
$attachments = get_children( array(
                        'post_type' => 'attachment',
                        'post_mime_type'=>'image',
                        'numberposts' => -1,
                        'post_status' => 'inherit',
                        'post_parent' => $post->ID
                    )
                );
if( $attachments ) {
   foreach ( $attachments as $attachment ) {
       echo wp_get_attachment_link( $attachment->ID, '' , TRUE, FALSE, 'Link to image attachment' );
   }
} else {
   echo ''; //if no attachment found
}

Please note that I've changed in 4 positions:

  1. Removed the first if() condition and put it below
  2. Made the 'numberposts' to 'all' using a -1
  3. Changed the 'post_status' to 'inherit', as attachments are in that status
  4. And most importantly in your case, changed the number of posts (in posts_per_page) to 5, because you need 5 images.

But the code will fetch only 5 image-link, though there can be a lot. And you are actually getting showing 5 links only.

EDIT #2

You are missing a basic thing. When inserting an image into a post content, where you want to let the user go is up to you, because you have the control there. Image insertion in Content area targeting attachment template

No need to do any query or linking. It's that simple. :)

  • This adds the link to the featured thumbnail I want also to the rest of the images inserted in the post to point to the attachment page template, there are about 5 images. – Adrian Oct 24 '14 at 10:08
  • @user3467855 Okay, see my edit, please – Mayeenul Islam Oct 24 '14 at 12:21
  • First problem, number of images should be a variable, don't know how many the user wants to insert in his post. Seconds, the code is echoing now a link to the first image. I need somehow to attach for each inserted image in the post the link to the attachement page. The images are found in the the_content() function, I can't just add that link. I am thinking it has to do with functions.php. So the final output: WHEN i click on an image from the post need to direct me to the attachment.php. – Adrian Oct 24 '14 at 14:04
  • For example what I see when I hover or click on the image domain.com/wp-content/uploads/2014/10/Elements-Optimal-So-Table-01.jpg, and I need to see smth like domain.com/name-of-the-image ... or smth like that. – Adrian Oct 24 '14 at 14:05
  • Ok. I edited again. See Edit #2. But I fixed one of my mistake on Edit... the numberposts was right; and I set it to all. :) – Mayeenul Islam Oct 24 '14 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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