Title: Permalink in FIRST image?
Last modified: August 19, 2016

---

# Permalink in FIRST image?

 *  Resolved [pyrrho](https://wordpress.org/support/users/pyrrho/)
 * (@pyrrho)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/permalink-to-first-image/)
 * I want the first image in a post to automatically link to the ‘post page’ (the
   full article), but so far, I haven’t been succesful.
 * I installed the Post Image plug-in from Guff, but I am having some trouble with
   it. If I put this in the loop…
 *     ```
       <?php post_image(); ?>
       <?php the_content('>>>'); ?>
       <?php edit_post_link('Edit article', ''); ?>
       ```
   
 * …I see the image TWO times. The first image DOES link to the full article, but
   it is placed at the wrong position (above the text).
 * Maybe somebody could share some tips?

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

 *  [wp_guy](https://wordpress.org/support/users/wp_guy/)
 * (@wp_guy)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/permalink-to-first-image/#post-759262)
 * I once faced the same dilemma ^_^.
 * Here is the code that I came up with:
 *     ```
       function link_image($content){
   
       $searchfor = '/(<img[^>]*\/>)/';
       $replacewith = '<a href="'.get_permalink().'">$1</a>';
   
       if (is_single() === FALSE){
       	$content = preg_replace($searchfor, $replacewith, $content, 1);
       }
       return $content;
   
       }
   
       add_filter('the_content', 'link_image');
       ```
   
 * Copy and paste that code into your functions.php file, and you’re all set.
 * Cheers!
 *  Thread Starter [pyrrho](https://wordpress.org/support/users/pyrrho/)
 * (@pyrrho)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/permalink-to-first-image/#post-759278)
 * Thanks for your code! I get an error, though:
 * **Call to undefined function: add_filter() in …/wp-includes/functions.php on 
   line 15**
 * Any idea how to define the add_filter function?
 *  [ivovic](https://wordpress.org/support/users/ivovic/)
 * (@ivovic)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/permalink-to-first-image/#post-759279)
 * you’re putting the line into the wrong functions.php.
 * you neeed to put that into a functions.php in your THEME folder. If there isn’t
   already an existing functions.php there, make one.
 * Dynamic duo out.
 *  Thread Starter [pyrrho](https://wordpress.org/support/users/pyrrho/)
 * (@pyrrho)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/permalink-to-first-image/#post-759280)
 * That was… very stupid. 😉
 * Thank you very much (wp_guy AND Ivovic). It works!
 *  [ivovic](https://wordpress.org/support/users/ivovic/)
 * (@ivovic)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/permalink-to-first-image/#post-759281)
 * That’s actually quite an elegant solution, I’m gonna have to make a note.
 * *tips hat to wp_guy*
 *  [wp_guy](https://wordpress.org/support/users/wp_guy/)
 * (@wp_guy)
 * [18 years, 1 month ago](https://wordpress.org/support/topic/permalink-to-first-image/#post-759342)
 * Thanks 😎

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

The topic ‘Permalink in FIRST image?’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 3 participants
 * Last reply from: [wp_guy](https://wordpress.org/support/users/wp_guy/)
 * Last activity: [18 years, 1 month ago](https://wordpress.org/support/topic/permalink-to-first-image/#post-759342)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
