0

I am using the advanced excerpt plugin for Wordpress http://wordpress.org/plugins/advanced-excerpt/ so I can display images and video's in my excerpts, as opposed to just text.

So my code is simply...

<?php the_excerpt(); ?>

Now if the excerpt is an image and text, I would like to make only the image tag an anchor link, not the text.

If I do this...

<a href="<?php the_permalink(); ?>">
    <?php the_excerpt(); ?>
</a>

...it makes the whole excerpt an anchor link as you would expect. The trouble with this is the paragraph text then inherits the link colour which I do not want, and if I disable this colour just for this excerpt, it also disables intended anchor links.

Is there any way I can do this in jQuery or PHP to select only the image in the excerpt and make it link to the corresponding blog post?

Thanks in advance

1

To get a link around only part of the excerpt content you will need a filter on get_the_excerpt similar to the following:

function dummy_excerpt_filter($excerpt) {
  // manipulate the excerpt
  return $excerpt;
}
add_filter('get_the_excerpt','dummy_excerpt_filter');

The problem with that is that the // manipulate the excerpt part means using regex on your excerpt content, which can be dicey. Before diving into that I am going to suggest another solution.

Instead of this...

<a href="<?php the_permalink(); ?>">
    <?php the_excerpt(); ?>
</a>

Do this...

<a class="excerpt-link" href="<?php the_permalink(); ?>">
    <?php the_excerpt(); ?>
</a>

You can now use the excerpt-link class to control the appearance specifically of the links in the excerpt. That may get you were you want to go without having to regex markup.

Since you seem to be editing the template anyway, you could also use:

$excerpt = get_the_excerpt();
// manipulate and echo the excerpt however you want.

Though that still may mean using regex on markup.

  • Thanks for the reply, but the class on the anchor wont really help. The whole text will become a link which I do not want. There could be a genuine link in that text which gets overwritten. – Adam Jul 1 '13 at 19:08
  • Then use the first suggestion-- the filter. Edited the answer with another possibility. – s_ha_dum Jul 1 '13 at 19:19
  • Thanks but I have never used regex, however looking into it I may be able to figure it out, thanks for pointing me in the right direction anyway. – Adam Jul 3 '13 at 10:37

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.