0

Is there an existing plugin that allows a person to, in a post, put a link on an image, but when the post is shown as part of the main-page list, have that image link to the post's permalink instead.

If not, can you point me in the right direction to custom code this myself? (What files to edit, what WP functions already exist that might help me...) I have a decent grasp of functional/procedural PHP, but have only the slightest experience editing WP code.

I am using WordPress 3.9 and the theme "Twenty Ten"

Feel free to re-tag this question. I am not familiar enough with WP or WP.SE to know what tags are best.

6
  • Depends on where you're posts are showing up, could be index.php or some kind of archive.php - are these posts you're referring to "Posts" or are they "Pages"? or something different?
    – Howdy_McGee
    Commented May 7, 2014 at 20:36
  • They are posts. The edit link looks like this: wp-admin/post.php?post=1531&action=edit On this site "Pages" is empty. The place where I want the images to link to the permalinks is the main page of the site.
    – TecBrat
    Commented May 7, 2014 at 21:01
  • Just remember, asking for plugin/product recommendations are off topic. Commented May 8, 2014 at 18:59
  • 1
    @PieterGoosen, I saw that when I looked for a good tag, but thought that "If not, can you point me in the right direction to custom code this myself?" would offset that well enough. I ended up doing just that, and have answered my own question.
    – TecBrat
    Commented May 8, 2014 at 19:06
  • @PieterGoosen The edit you did on my title changes the meaning of it. I'm new enough here that I REALLY don't want to get into an edit war, so I ask you to please reconsider your edit.
    – TecBrat
    Commented May 8, 2014 at 19:10

1 Answer 1

0

My solution was to work in the twentyten theme folder, the file "loop.php"

Around line 145 I did this:

<?php $content = get_the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyten' ) );
  // http://stackoverflow.com/a/6366390/1195835
  // http://stackoverflow.com/a/11886237/1195835
  // http://stackoverflow.com/a/15469353/1195835
  $doc = new DOMDocument();
  $doc->loadHTML($content);
  $finder = new DomXPath($doc);
  $classname = "post-main-image";
  $nodes = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");      
  foreach($nodes as $node){
   $node->setAttribute('href',get_the_permalink());
  }
  echo $doc->saveHTML();
?>

Then in the edit post screen, I simply type "post-main-image" in the "Link CSS Class" form field and update. I would expect something similar to work in other themes as well.

Your Answer

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

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