Search code examples
phpwordpressloopswidget

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

Solution

  • 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