Excerpt not working

Hi,

I’m trying to add an excerpt to my recent posts but it doesn’t appear to be working.

I’ve added the following code to my functions.php in the x-child folder

   // 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">'
                       . '<h3 class="h-recent-posts">' . get_the_title() . '</h3>'
                       . '<span class="x-recent-posts-date">' . get_the_date() . '</span>'
                        . $excerpt
                     . '</div>'
                   . '</div>'
                 . '</article>'
               . '</a>';

    endwhile; endif; wp_reset_postdata();

  $output .= '</div>';

  return $output;
}

I’ve added a text element in cornerstone and added the following shortcode:

[recent_posts type="post" enable_excerpt="true" count="1" orientation="horizontal" category="Bestuur"]

The excerpt doesn’t show up.

What I want is to create something similar to this:

So far I have managed to get the photo and the title right, but not the date or excerpt.

Can someone tell me what I can try?

Hello Vink,

Thanks for asking. :slight_smile:

Please take a look at following solution and let us know the outcome.

In case problem is still there, please share website login details in secure note for us to take a closer look.

Thanks.

As I mentioned in my post, I did do this already. I will send you my login details in a secure message.

Hi Vink,

I have created a test page on your site and I see that your recent post excerpts are functioning properly. Please check the test page link that I have added to the secure note below and see how I have added the shortcode into a raw content element.

Thanks!

Hi,

Thanks for looking into it.

I have tried the raw content thing on my homepage now which seems to show the excerpt. It however doesn’t show the date. How can I add that in like it has been added to the test you created?

Also I want the excerpt to have a background and the text should be white, how can I create that? Because that doesn’t seem to work either.

The date was hidden by your custom CSS in your child theme.

Once that’s removed, the date will show and you can color your date and excerpt white using this code.

.x-recent-posts-date,
.x-recent-posts-excerpt,
.x-recent-posts-excerpt * {
    color: white !important;
}

I won’t recommend applying a background to the excerpt. Apply the background to the whole content instead.

.x-recent-posts .x-recent-posts-content {
    padding: 10px 5px;
    background: rgb(202,0,61);
}

This would be the result:

Looks like you will need to adjust the paddings also.

As you can see here, this requires a lot of customization to achieve what you need. I’d strongly recommend that you use either The Grid or Essential Grid for this because both plugins has a skin editor or builder.

Custom coding is also not a part of our support. The codes given here in the forum only serve as a guide. We can’t maintain them and offer support if they break and if you need further customization.

Hope that helps and thank you for understanding.

Thanks for the tips.

I have customised it to my liking now. I understand that any further customisation isn’t part of the support. It was mostly about the excerpt itself not showing up, and that has been resolved!

Thanks!

Glad to hear it’s working now.

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