Multiple Excerpt Lengths

I’m needing to set multiple excerpt lengths: one for the recent posts shortcode and one for the main blog. I have code in my functions.php to allow for an excerpt to show with the recent posts shortcode.

For the shortcode I’d like the excerpt to be only 25, but for the main blog page it should be 220.

I also have some code to limit the excerpt length for my events widget, which I’m doing by post type:

function custom_excerpt_length( $length ) {
global $post;
if ($post->post_type == 'tribe_events')
return 20;
else
return 220;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

Is there a way I can have one excerpt length set for my events widget, one for the recent posts shortcode, and one for the main blog page?

Hello @afloeckner.metova,

Thanks for writing in!

You can set a default excerpt length for the posts in X > Theme Options > Blog > Content > Excerpt Length.

And to set the excerpt length for the events, you can make use of this code:

function custom_excerpt_length( $length ) {
	if ( get_post_type() == 'tribe_events'){
		return 20;
	}

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

We would loved to know if this has work for you. Thank you.

Hi! So this solves my problem for the events, but not for the recent posts shortcode and blog page.

I already have the excerpt length in X > Theme Options > Blog > Content > Excerpt Length set to 220. But I need the excerpt length for my recent posts shortcode to be 25. Right now it’s also showing 220, but that is way to long for it’s location on the homepage. Is there a way to set an excerpt length just for the recent posts shortcode?

Hi there,

This depends on the code that you have added to the child theme for have the recent post shortcode display the excerpts. Would you mind posting the code you currently have so that we can provide further suggestion?

Thank you.

Yes! It is below. I pulled it from another support thread I found.

// Add Excerpts to the recent post element
// =============================================================================
function x_shortcode_recent_posts_v2code( $atts ) {
  extract( shortcode_atts( array(
    'id'           => '',
    'class'        => '',
    'style'        => '',
    'type'         => 'post',
    'count'        => '',
    'category'     => '',
    'offset'       => '',
    'orientation'  => '',
    'show_excerpt' => 'false',
    'no_sticky'    => '',
    'no_image'     => '',
    'fade'         => ''
  ), $atts, 'x_recent_posts' ) );

  $allowed_post_types = apply_filters( 'cs_recent_posts_post_types', array( 'post' => 'post' ) );
  $type = ( isset( $allowed_post_types[$type] ) ) ? $allowed_post_types[$type] : 'post';

  $id            = ( $id           != ''     ) ? 'id="' . esc_attr( $id ) . '"' : '';
  $class         = ( $class        != ''     ) ? 'x-recent-posts cf ' . esc_attr( $class ) : 'x-recent-posts cf';
  $style         = ( $style        != ''     ) ? 'style="' . $style . '"' : '';
  $count         = ( $count        != ''     ) ? $count : 3;
  $category      = ( $category     != ''     ) ? $category : '';
  $category_type = ( $type         == 'post' ) ? 'category_name' : 'portfolio-category';
  $offset        = ( $offset       != ''     ) ? $offset : 0;
  $orientation   = ( $orientation  != ''     ) ? ' ' . $orientation : ' horizontal';
  $show_excerpt  = ( $show_excerpt == 'true' );
  $no_sticky     = ( $no_sticky    == 'true' );
  $no_image      = ( $no_image     == 'true' ) ? $no_image : '';
  $fade          = ( $fade         == 'true' ) ? $fade : 'false';

  $js_params = array(
    'fade' => ( $fade == 'true' )
  );

  $data = cs_generate_data_attributes( 'recent_posts', $js_params );

  $output = "<div {$id} class=\"{$class}{$orientation}\" {$style} {$data} data-fade=\"{$fade}\" >";

    $q = new WP_Query( array(
  'orderby'             => 'date',
  'post_type'           => "{$type}",
  'posts_per_page'      => "{$count}",
  'offset'              => "{$offset}",
  "{$category_type}"    => "{$category}",
  'ignore_sticky_posts' => $no_sticky
) );

if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post();

  if ( $no_image == 'true' ) {
    $image_output       = '';
    $image_output_class = 'no-image';
  } else {
    $image              = wp_get_attachment_image_src( get_post_thumbnail_id(), 'entry-cropped' );
    $bg_image           = ( $image[0] != '' ) ? ' style="background-image: url(' . $image[0] . ');"' : '';
    $image_output       = '<div class="x-recent-posts-img"' . $bg_image . '></div>';
    $image_output_class = 'with-image';
  }

   $excerpt = ( $show_excerpt ) ? '<div class="x-recent-posts-excerpt"><p style="color:initial;">' . preg_replace('/<a.*?more-link.*?<\/a>/', '', cs_get_raw_excerpt() ) . '</p></div>' : '';

  $output .= '<a class="x-recent-post' . $count . ' ' . $image_output_class . '" href="' . get_permalink( get_the_ID() ) . '" title="' . esc_attr( sprintf( __( 'Permalink to: "%s"', 'cornerstone' ), the_title_attribute( 'echo=0' ) ) ) . '">'
             . '<article id="post-' . get_the_ID() . '" class="' . implode( ' ', get_post_class() ) . '">'
               . '<div class="entry-wrap">'
                 . $image_output
                 . '<div class="x-recent-posts-content">'
	   . '<span class="x-recent-posts-date">' . get_the_date() . '</span>'
                   . '<h3 class="h-recent-posts">' . get_the_title() . '</h3>'
                    . $excerpt
	  . '<span class="read-more">Read More</span>'
                 . '</div>'
               . '</div>'
             . '</article>'
           . '</a>';

endwhile; endif; wp_reset_postdata();
  $output .= '</div>';

  return $output;
}



// =============================================================================
add_action('wp_head', 'change_recent_posts_to_v2');
function change_recent_posts_to_v2() {
  remove_shortcode( 'x_recent_posts' );
  add_shortcode( 'x_recent_posts', 'x_shortcode_recent_posts_v2code' );
}

Hi @afloeckner.metova,

The excerpt lenght option is still working fine when I used the recent post shortcode:

Could you please try changing it to 10 to check if it works or NOT?

If it still doesn’t help, would you mind providing us with login credentials(by clicking on the Secure Note button at the bottom) so we can take a closer look? To do this, you can make a post with the following info:

  • Link login to your site
  • WordPress Admin username / password

Thanks.

Guys, I feel like you’re miss-understanding or I’m doing a bad job at explaining.

The excerpt is working. It’s working for both the recent posts shortcode and the main blog page. Right now I have the excerpt length set to 220 in the Theme Options. What I need though is to set a separate excerpt length just for the shortcode. I don’t want 220 for the shortcode, that’s way too long for where it’s located. I only want about 20 for the shortcode, but want to keep 220 set for the blog page. I will share temp login info securely so you can check it out.

Hi,

In your recent post code

// Add Excerpts to the recent post element

Please change this line

$excerpt = ( $show_excerpt ) ? '<div class="x-recent-posts-excerpt"><p style="color:initial;">' . preg_replace('/<a.*?more-link.*?<\/a>/', '', cs_get_raw_excerpt() ) . '</p></div>' : '';

with this

$excerpt = ( $show_excerpt ) ? '<div class="x-recent-posts-excerpt"><p style="color:initial;">' . preg_replace('/<a.*?more-link.*?<\/a>/', '', substr(cs_get_raw_excerpt(),0,55) ) . '...</p></div>' : '';

Hope that helps

This is better! Its close, but not exactly what I was hoping for. It would be nice to set a word limit like 20 or 25 so that a bit more text can show instead of just one line. Do you think this is possible?

Hey @afloeckner.metova,

Edit the length in the substr function. See http://php.net/manual/en/function.substr.php.

Please note that displaying excerpt in the Recent Post shortcode is not an officially supported feature. The custom codes provided here are only to provide guidance to help you get started with extending the theme. The maintenance including fixing custom codes and further enhancing them are not included in our product support.

If you have further needs regarding the excerpt, I’d recommend that you use one of the bundled grid plugins as they allow the creation of custom skins including excerpt limitations.

The Grid

Essential Grid:

For more details, please visit the link below.

The code from paul.r is working great and functioning as expected. Thanks for all the help with this!

You are most welcome. :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.