0

Struggling here with the excerpt and options for it.
(NOT looking for a plugin and not extracting from the content, we add seperate excerpt content)

We want to have an excerpt limited to a max. amount of words, using HTML code when needed and showing an image.

The theme we use didn't have the excerpt option so we add following:

<?php the_excerpt(); ?> 

in the template. It now shows the excerpt, but it does not limit the output. (characters nor wordcount)

Our next step is to solve the length "issue", I thought I was being being smart and used this snippet in functions.php (from codex itself):

function custom_excerpt_length( $length ) {
return 50;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

Didn't do anything, maybe I'm overlooking something?! Already feeling dumb but still going on..

So I tried following code in the template itself. (replaced the_excerpt(); and taken out the codex function).

<?php echo wp_trim_words( get_the_excerpt() , 50, "... <a class='read-more' href='". get_permalink() ."'>read more..</a>" ); ?>

Nice seems to work eh..not as thought, this code is limiting words (as wanted) to max 50 words but stripping html code and not allowing images...argh.

So my question is: What code can I use which is able to limit the excerpt (to max Ie. 50 words), allows HTML and allows me to display an image and showing the ...read more link for our excerpt.

Note: tags aint as they should be because my reputation is not high enough..

  • 1
    A plugin is the way to go. It's adding functionality, so shouldn't be in the theme (i.e. functions.php), and I'm sure this is well-trodden ground. Any reason you want to avoid a plugin? – markcbain Dec 13 '13 at 6:28
  • the_excerpt() does not shorten the manually typed excerpt text; I agree with @markcbain that a plugin is the easiest way to get what you want. – Michael Dec 13 '13 at 12:07
  • @markcbain a plugin is not our way to go, thank you. @Michael yes I know by now that get_the_excerpt is what I need to use, thank you for mentioning it. – Charles Dec 14 '13 at 13:34
  • @Charles what you mean is "someone else's plugin is not our way to go". Reading your comment below, you are building you own plugin! – markcbain Dec 20 '13 at 6:12
0

Answer on my own question (without using a plugin and choosing a middle way) for what we want to achieve.

Because the template has also a code starting with:

if ( has_post_thumbnail() && ( $post->post_type == 'post' ) )

we have taken that part of code away. (we won't used it in postings/pages, therefore)

Following part of code (below) in the template allows us:

  • use of the feature image option, which will show that image on the excerpt only
  • character count (not word count but works), which is the, as mentioned, middle way
  • showing the permalink (read more..)
  • use of <p> <br> <pre> and some more

Maybe it is not flawless but seems to work great although (maybe) not common. A part of that code is found here second excerpt.

If someone thinks it could trigger some error or would handle the search option in a nasty way, I invite you to comment and help us to make it in that case "foolproof".
Thank you.

if (is_home() ||is_front_page() || is_archive() || is_search() || is_category()){
the_post_thumbnail('thumbnail', array('class' => 'excerpt_img '));  
echo substr( get_the_excerpt(), 0, strrpos( substr( get_the_excerpt(), 0, 500),''));
echo '<a class="read-more" href="'. get_permalink( get_the_ID() ).'"> .... Read More</a>';

Note:
For whatever reason the plugin as mentioned by markcbain did not work for us anyway!
Btw we don't use code-snippets in a theme functions.php we use mu-pugins if code is needed for more then just one theme and/or a plugin "code-snippets" for other usesfull code.

  • glad to see we in fact agree that code-snippets should go in a plugin rather than functions.php:) – markcbain Dec 20 '13 at 6:15
  • ???, these are 4 lines hardcoded in (child)template. – Charles Dec 20 '13 at 6:18
  • edited my comment. Was meaning in general, not in this case. – markcbain Dec 20 '13 at 6:28
  • all okay :), you are a good/or experienced coder? would be nice to have a helping hand at another question. Be my guest Mark – Charles Dec 20 '13 at 6:33
-1

I'd try this plugin: http://wordpress.org/plugins/advanced-excerpt/, which seems to offer what you are looking for. Yes, I know you said no plugins, but you didn't say why, so I think it's worth mentioning.

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.