简体   繁体   中英

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 -->

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 );

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

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

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'); ?>

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); ?>

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

<?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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM