0

I've written a loop that displays all the images that are attached to every single post of a specific post type. The post type is called "atls_events" and I'm outputting an image size called "video-thumb" for each image attached to each post. I have all of that working. You can see the code here:

<?php 

$query = new WP_Query( 
    array( 
        'post_type' => 'atls_event', 
        'posts_per_page' => -1,
        'fields' => 'ids'
    ) 
    );
$image_query = new WP_Query( 
    array( 
        'post_type' => 'attachment', 
        'post_status' => 'inherit', 
        'post_mime_type' => 'image', 
        'posts_per_page' => -1, 
        'post_parent__in' => $query->posts, 
        'order' => 'DESC' 
    ) 
    );

if( $image_query->have_posts() ){
while( $image_query->have_posts() ) {
$image_query->the_post();?>

    <div class="m-1of2 t-1of3 d-1of6 cf">
        <?php $imgurl =  wp_get_attachment_image($image->ID, 'video-thumb');
             echo '<a href="';
             echo get_permalink(); //this is where I try to link to the post
             echo '">';
             echo $imgurl;
             echo '</a>';?>
    </div>

<?php } } ?>

What I'm tring to do is link each thumbnail to the post the image is attached to. I'm attempting to do this with the following piece of code:

<?php $imgurl =  wp_get_attachment_image($image->ID, 'video-thumb');
 echo '<a href="';
 echo get_permalink(); //this is where I try to link to the post
 echo '">';
 echo $imgurl;
 echo '</a>';?>

However this links the image to the attachment page of the specific image. How do I link the image to the post itself?

1
  • Sorry, will concentrate more on formatting next time. Thanks! Dec 17, 2014 at 15:32

1 Answer 1

1

You have two issues here.

  • The use of $image->ID in this line

    wp_get_attachment_image($image->ID, 'video-thumb');
    

This will give you the following error:

NOTICE Error: [8] Undefined variable: image

In all honesty, I think that should actually be $post->ID

  • get_permalink() returns the permalink to the current post, which in this case is the image. If you want to link back to the post parent, you can pass the post parent ID to get_permalink(). The post parent ID can be retrieved with $post->post_parent, so something like this will do

    get_permalink( $post->post_parent );
    
1
  • I wasn't receiving any errors using $image->ID however I've changed it anyways. Also get_permalink( $post->post_parent ); works, will mark as correct answer, thanks! Dec 18, 2014 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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