0

I've already checked out this post (Multiple excerpt lengths in wordpress) but it's obviously for versions of WordPress many many times removed from where we're at now. I need multiple lengths for different areas as well, but with tags stripped out. I don't use the "excerpt" box on a post, so it needs to concatenate the content itself. I very much liked the suggested answer on that page that included "short", "regular" and "long", but I can't seem to get it to work.

I'm using this in a shortcode loop on my homepage that displays the first post in a group a different way than the remaining 4 posts. I'm only including the code up through the end of the first post since the others do not include an excerpt.

This is what I currently have. Maybe someone can see where I'm messing things up:

FUNCTIONS.PHP class Excerpt {

// Default length (by WordPress)
public static $length = 55;

// So you can call: my_excerpt('short');
public static $types = array(
  'short' => 25,
  'regular' => 55,
  'long' => 100
);

/**
* Sets the length for the excerpt,
* then it adds the WP filter
* And automatically calls the_excerpt();
*
* @param string $new_length 
* @return void
* @author Baylor Rae'
*/
public static function length($new_length = 55) {
Excerpt::$length = $new_length;

add_filter('excerpt_length', 'Excerpt::new_length', 999);

Excerpt::output();
}

// Tells WP the new length
public static function new_length() {
  if( isset(Excerpt::$types[Excerpt::$length]) )
    return Excerpt::$types[Excerpt::$length];
  else
    return Excerpt::$length;
}

// Echoes out the excerpt
public static function output() {
  the_excerpt();
}

}

// An alias to the class
function my_excerpt($length = 55) {
  Excerpt::length($length);
}

RECENT POSTS

                <?php $args2 = array(
                    'post_type' => 'post',
                    'posts_per_page' => 5,
                    'orderby' => 'date',
                    'order' => 'DESC',
                    'post_status' => 'publish',
                );
                $recent_posts = wp_get_recent_posts( $args2 );
                $count2 = 0;

                foreach( $recent_posts as $recent ){
                    $count2++;
                    if ($count2 == 1) { ?>

                <div class="col-xs-12 col-sm-6 col-md-6">
                    <div class="first-news clearfix">
                        <a href="<?php echo get_permalink($recent["ID"]); ?>" title="<?php echo $recent["post_title"]; ?>">
                            <?php $feat_image3 = wp_get_attachment_url( get_post_thumbnail_id($recent["ID"]) ); ?>
                            <div class="post-thumbnail" style="background-image: url(<?php echo $feat_image3; ?>); height: 200px;">

                                <?php $format = get_post_format($recent["ID"]);
                                    if ( false === $format ) { ?>
                                        <span class="standard overlay-icon"><i class="fa fa-search fa-2x"></i></span>                                                   
                                    <?php }

                                    if ( 'aside' === $format ) { ?>
                                        <span class="aside overlay-icon"></span>
                                    <?php } 

                                    if ( 'image' === $format ) { ?>
                                        <span class="image overlay-icon"><i class="fa fa-photo fa-2x"></i></span>
                                    <?php }

                                    if ( 'video' === $format ) { ?>
                                        <span class="video overlay-icon"><i class="fa fa-video-camera fa-2x"></i></span>
                                    <?php }

                                    if ( 'quote' === $format ) { ?>
                                        <span class="quote overlay-icon"><i class="fa fa-quote-left fa-2x"></i></span>
                                    <?php }

                                    if ( 'link' === $format ) { ?>
                                        <span class="link overlay-icon"><i class="fa fa-link fa-2x"></i></span>
                                    <?php }

                                    if ( 'gallery' === $format ) { ?>
                                        <span class="gallery overlay-icon fa-stack">
                                            <i class="fa fa-image fa-stack-1x fa-nudge-right fa-nudge-down fa-outline-inverse" style="font-size:1.5em" ></i>
                                            <i class="fa fa-image fa-stack-1x fa-outline-inverse"  style="font-size:1.5em" ></i>
                                            <div style="background-color:#FFF;width:70%;height:55%;left:4%;top:10%;position:absolute"></div>
                                            <i class="fa fa-image fa-stack-1x fa-nudge-left fa-nudge-up fa-outline-inverse" style="font-size: 1.5em"></i>
                                        </span>
                                    <?php } 

                                    if ( 'status' === $format ) { ?>
                                        <span class="status overlay-icon"><i class="fa fa-sticky-note fa-2x"></i></span>
                                    <?php }

                                    if ( 'audio' === $format ) { ?>
                                        <span class="audio overlay-icon"><i class="fa fa-volume-up fa-2x"></i></span>
                                    <?php }

                                    if ( 'chat' === $format ) { ?>
                                        <span class="chat overlay-icon"><i class="fa fa-commenting fa-2x"></i></span>
                                    <?php }

                                    if ( 'code' === $format ) { ?>
                                        <span class="code overlay-icon"><i class="fa fa-code fa-2x"></i></span>
                                    <?php }
                                ?>
                            </div>
                        </a>

                        <a title="<?php the_title($recent["ID"]) ?>" href="<?php echo get_permalink($recent["ID"]); ?>" rel="bookmark">
                            <h3 class="post-box-title"><?php echo get_the_title($recent["ID"]); ?></h3>
                        </a>

                        <p class="post-meta">
                            <span class="darwin-date">
                                <i class="fa fa-clock-o"></i>
                                <?php echo get_the_time('M d, Y', $recent["ID"]); ?>
                            </span>
                        </p>

                        <div class="entry">
                            <p>
                                <?php echo my_excerpt('short', $recent['ID']); ?>
                            </p>

                            <div class="more-posts">
                                <a class="more-link" title="<?php echo get_the_title($recent["ID"]); ?>" href="<?php the_permalink($recent["ID"]); ?>">Read More &raquo;</a>
                            </div>
Community
  • 1
  • 1
Laura Sage
  • 33
  • 7

1 Answers1

0

You can trim excerpt with native Wordpress function wp_trim_words.

More here wp_trim_words

Tusko Trush
  • 422
  • 3
  • 15
  • I've tried that. It's not working with my $recent['ID'] part, which has to be there since it's outside of the loop, from what I can tell. In other words, this is part of a shortcode, not on a blog page (I'm probably not explaining myself right here. Extremely fatigued at the moment). – Laura Sage Oct 17 '16 at 18:30
  • with `wp_trim_words` you can use any text what U want. For example `wp_trim_words( $recent['post_content'], 20, '...' )` – Tusko Trush Oct 17 '16 at 18:35
  • I've tried that before. It didn't work with the $recent["ID"] component of the loop though. At least, I couldn't get it to. – Laura Sage Oct 17 '16 at 19:19
  • I apologize for the double entry. Ah HA! I had not had the correct for mat for that. Trying it now. – Laura Sage Oct 17 '16 at 19:30
  • 1
    That worked like a charm. Thank you. I kept trying it with $recent['ID'], 20, '...', and it didn't work. Had not thought to change ID to post_content. – Laura Sage Oct 17 '16 at 19:33