The Grid post excerpt length gets overridden by customizer settings

Hi there,

on my page I’m using the Grid to display the latest blog post:

Because of the layout I would like it to show a longer excerpt.
I found the settings in the skin builder but somehow they won’t apply.

That might be because I have some of your custom code added to modify the recent post element (to show an excerpt and “read more” link). Besides I don’t need the “read more” link anymore.

Is there a way to give the grid excerpt settings priority?

Thank you

Hi There,

Thanks for writing in! Could you please post your custom code that you have added with regard to excerpts, so that we can test it on our end and provide a workaround for this.

Thanks!

Sure, here it is:

  // Displaying Excerpt in Recent Posts
// =============================================================================

function x_shortcode_recent_posts_v2code( $atts ) {
  extract( shortcode_atts( array(
    'id'           => '',
    'class'        => '',
    'style'        => '',
    'type'         => 'post',
    'count'        => '',
    'category'     => '',
    'offset'       => '',
    'orientation'  => '',
    'show_excerpt' => 'true',
    '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>' . preg_replace('/<a.*?more-link.*?<\/a>/', '', cs_get_excerpt_for_social() ) . '</p></div>' : '';

      $output .= '<a class="x-recent-post' . $count . ' ' . $image_output_class . '" href="' . get_permalink( get_the_ID() ) . '" title="' . esc_attr( sprintf( csi18n('shortcodes.recent-posts-permalink'), 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">'
                       . '<h3 class="h-recent-posts">' . get_the_title() . '</h3>'
                        . $excerpt
                     . '</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' );
}

March 2021 Update: The above code example has been adjusted to account for a change to a function name.

Hi,

You can try adding this to your child theme’s functions.php

 function x_excerpt_length( $length ) {
  if (is_page(9)){
        return 200;
   }
   return x_get_option( 'x_blog_excerpt_length' );

  }
  add_filter( 'excerpt_length', 'x_excerpt_length' );

Change 200 to your desired excerpt length.

Hope that helps

Hi there,

This is doing the same like changing the excerpt length in the customizer.
But still when I’m using the grid with a longer excerpt length than set in the customizer it won’t show.

The problem is, I need both: a specific excerpt length for recent posts and a different (longer) excerpt length for a single “The Grid”. The Grid already has the option to set the excerpt length but it will only show a maximum of words that’s set in the customizer.

Hello There,

The code isn’t doing anything to the page because it is referencing to a wrong page. The correct page ID should be 250. Please use this code instead:

 function x_excerpt_length( $length ) {
  if ( is_page(250) ){
        return 124;
   }
   return x_get_option( 'x_blog_excerpt_length' );

  }
  add_filter( 'excerpt_length', 'x_excerpt_length' );

And to find the page ID, please check this article:

Hi there,

thanks for getting back to me.

Still it’s not doing the trick.
I want the recent posts to have an excerpt length of 18 words - that works.
BUT I want the excerpt of the The Grid element ([the_grid name=“Top Post Content”]) to be longer (604 characters).

That’s somehow not working and always limited to the general excerpt settings.

Hello There,

Regretfully this is the limitation of the grid plugin. The excerpt length has a filter which is explained here: https://codex.wordpress.org/Plugin_API/Filter_Reference/excerpt_length

The code we gave will customize the excerpt length when you are on the page. The problem is that the grid settings will not override the general settings. It is still dependent on the theme settings just as it has been given when you edit the grid skin.

Thank you for your understanding.

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