jthespartan
jthespartan

Reputation: 63

How to limit an excerpt in wordpress?

I'm using the Events Calendar by Modern Tribe in my website and I want to modify the widget. I have everything the way I want it except for one small detail... the excerpt. This is how the section of my code looks in my website, which pulls the events description. But I don't want the full description for the widget... I only one 10 words or so, followed by an ellipse (...). Any thoughts?

<div class="entry-content tribe-events-event-entry" itemprop="description">
    <?php if (has_excerpt ()): ?>
        <?php the_excerpt(); ?>
    <?php else: ?>
        <?php the_content(); ?>
    <?php endif; ?>
</div> <!-- End tribe-events-event-entry -->

Upvotes: 3

Views: 27044

Answers (9)

zlatko z
zlatko z

Reputation: 11

Just replace

<?php the_excerpt(); ?>

with

<?php echo wp_trim_words(get_the_content(), 20); ?>

where 20 is the number of words from the content you want to show.

Upvotes: 1

ah jony
ah jony

Reputation: 31

<?php 
$excerpt = get_the_excerpt();
$excerpt = substr( $excerpt , 0, 100); 
echo $excerpt;
?>

You can change the amount to something you like by changing the 100.

Upvotes: 1

csehasib
csehasib

Reputation: 365

I just found a solution to limiting the number of words in the excerpt without plugins. Add the following code to your functions.php file.

<?php
// Custom Excerpt 
function excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'...';
} else {
$excerpt = implode(" ",$excerpt);
} 
$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
return $excerpt;
}
?>

Now, instead of using the_content() or the_excerpt in your loop, use excerpt($limit) or content($limit). If you want to limit your excerpt to 25 words the code would look like this:

<?php echo excerpt(25); ?>

I got solution to limiting the number of words in the excerpt here

Upvotes: 0

happiweb
happiweb

Reputation: 11

add the following code functions.php

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

MOre info on the_excerpt can be found here: http://www.happiweb.net/2014/05/gioi-han-tu-trong-mo-ta-theexcerpt-cua.html

Upvotes: 1

Pieter Goosen
Pieter Goosen

Reputation: 9941

First of all, don't use space between a the conditional and (). if (has_excerpt ()): should be if (has_excerpt()):

You can just simply use

function pietergoosen_custom_excerpts($limit) {
    return wp_trim_words(get_the_content(), $limit, '<a href="'. esc_url( get_permalink() ) . '">' . '&nbsp;&hellip;' . __( 'Read more &nbsp;&raquo;', 'pietergoosen' ) . '</a>');
}

and add

<?php echo pietergoosen_custom_excerpts($limit); ?>

where you need to display an excerpt. Just replace $limit with the amount of words you would like to return.

Example to return 40 words

<?php echo pietergoosen_custom_excerpts(40); ?>

Upvotes: 1

Kesar Sisodiya
Kesar Sisodiya

Reputation: 1624

Put this code in function.php

 
function word_count($string, $limit) {
 
$words = explode(' ', $string);
 
return implode(' ', array_slice($words, 0, $limit));
 
}
 

Then

<?php the_content() ?> or <?php the_excerpt() ?> whatever you have the code replace with

this:-

<?php echo word_count(get_the_excerpt(), '30'); ?>

Upvotes: 1

Leandro Villagran
Leandro Villagran

Reputation: 300

You could use this to create custom excerpts

// get the post content     
<?php $content = get_the_content(); ?>
<?php
// get the first 80 words from the content and added to the $abstract variable
 preg_match('/^([^.!?\s]*[\.!?\s]+){0,80}/', strip_tags($content), $abstract);
  // pregmatch will return an array and the first 80 chars will be in the first element 
  echo $abstract[0] . '...';
    ?>  

I adapted this from this tutorial http://www.kavoir.com/2009/02/php-generating-summary-abstract-from-a-text-or-html-string-limiting-by-words-or-sentences.html hope it helps

Upvotes: 2

Lemon Drop
Lemon Drop

Reputation: 2133

You could use pure css and use this type of code to get an ellipsis at the end of 100px of text:

max-width: 100px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;

Upvotes: -1

iabramo
iabramo

Reputation: 192

Try adding this to your functions.php file. More info on the_excerpt can be found here: http://codex.wordpress.org/Function_Reference/the_excerpt

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

Upvotes: 8

Related Questions