Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include html tags within a wordpress excerpt?

Currently in a wordpress template, if you use the code the_excerpt() it will display the first 55 words of a post and strips all html from the post.

I need to include <a href... tags in the excerpt so that links are still visible.

Existing methods include:

  1. Hacking the wordpress core - definitely not an option.
  2. using a plugin - don't want to use, it's dependent on the developer keeping the plugin up to date
  3. writing code within functions.php to re-write the excerpt filter. - I'd prefer not to use this as it might have to be changed in future versions of WP

Is there a filter hook or other known method to include html easily without hacks?

All help is appreciated! Cheers.

like image 843
Daryl Avatar asked Jun 07 '11 05:06

Daryl


People also ask

How do I use excerpts in WordPress?

To add the excerpt on your blog post, go to Posts >> Add New or simply edit your existing post. Now, on the right options panel, click on the Excerpt option and add the excerpt for your posts. Once done, click on Publish/Update button to save your changes.

What is excerpt in HTML?

Displays the excerpt of the current post after applying several filters to it including auto-p formatting which turns double line-breaks into HTML paragraphs. It uses get_the_excerpt() to first generate a trimmed-down version of the full post content should there not be an explicit excerpt for the post.

How do I show read more links in WordPress excerpts?

Insert a Read More tag This method allows you to choose which posts have excerpts, and you can make the excerpt any length you want. If you're using the Visual editor, place your cursor at the end of the text that you want to be the excerpt. Click Insert Read More tag in the toolbar.


2 Answers

As I see it, you can only use method 2 and 3; both of them can be updated via WordPress' back-end with virtually no programming required which is ideal if you're going to be installing and using them on client sites.

Here's a tutorial with working code for method 3 -- http://aaronrussell.co.uk/legacy/improving-wordpress-the_excerpt/ and here's a plugin to use method 2 -- http://wordpress.org/extend/plugins/advanced-excerpt/

like image 157
stealthyninja Avatar answered Nov 07 '22 09:11

stealthyninja


I use the following statement sometimes to get the first 55 words of a post content.

implode(' ', array_slice(explode(' ', get_the_content()), 0, 55));
like image 31
Box Avatar answered Nov 07 '22 08:11

Box