How to: make the whole WordPress excerpt clickable

A client recently asked me to make the post excerpts in their blog page clickable. This seems like a reasonable request and the logical answer (if you know your WordPress basics) is to open the relevant template file (probably index.php or home.php in this case) and wrap the_excerpt(); in <a href="<?php the_permalink(); ?>".. (and so on).

Except there’s two problems with this method:

  1. WordPress formats the excerpt and wraps it in <p> tags
    Which means that you’re then wrapping your block level <p> in an inline <a>. I’m sure some of you either don’t know what this means or don’t care, but I do. Let’s call it nostalgia for the days of carefully ensuring the mark-up on every page was W3C compliant. ;)
  2. Links, which are allowed in an excerpt, break your code
    If you have a link in your excerpt, which is allowed in WordPress, the link in the excerpt will break the link wrapped around the excerpt. You then end up with things linked that you don’t want to be linked and things that you do want to be linked, not. Or something.

Solution one: add_filter()

There is a simple solution that makes use of the built in excerpt filter, allowing you to wrap the excerpt in a link before it gets marked-up, solving problem 1:

function clickable_excerpt( $excerpt ) {
	return '<a href="'. get_the_permalink() .'" class="excerpt">'. $excerpt .'</a>';
}
add_filter( 'get_the_excerpt', 'clickable_excerpt' );

(This is ideally placed in your theme functions.php file)

Solution two: add_filter() & strip_tags()

The only thing is this *doesn’t* solve problem 2. The effects of the broken link mark-up are less disastrous with this method, but not removed altogether. The simplest (dirtiest) solution to this, aside from not using links in your excerpts at all, is to remove HTML from the excerpt altogether:

function clickable_excerpt( $excerpt ) {
	return '<a href="'. get_the_permalink() .'" class="excerpt">'. strip_tags( $excerpt ) .'</a>';
}
add_filter( 'get_the_excerpt', 'clickable_excerpt' );

Bob’s your uncle: clickable WordPress excerpts without the invalid mark-up. :)

In which I nearly have a breakdown

Things have been a little weird recently here at Chez Jem. When I posted Enter title here I was struggling more than I let on. I was struggling to see the point of anything. I didn’t want to get out of bed in the morning, I didn’t see the point in going to work because […]

London, baby!

I am … hastily does the maths nearly 29 years old and this weekend I visited London as a tourist and used the underground for the first time. The capital city of my very own country and it’s taken me this long to go and visit it! On Thursday, Gaz and I went to see […]

Enter title here

Feel like I should blog to record some of the stuff that’s been going on lately but I’m struggling to formulate intelligent sentences. Fall back to list format! I came home on October 22nd to find Flymo dead and Rosie missing. I can only guess but I assume something managed to get into the garden. […]