How to redirect ‘file url’ to ‘post url’ i.e. in attachment.php

Link to the image file using wp_get_attachment_link( $id, $size, $permalink, $icon, $text ):

  • $id is the attachment ID, which, if using attachment.php, will simply be $post->ID
  • $size is the image size, e.g. full, large, medium, thumbnail
  • $permalink determines whether a link to the file or post is returned. Use false (which is the default) to link to the file
  • $icon refers to a mime-type icon. Leave false, which is default
  • $text displays a text link. Leave false, which is default