Excerpt in recent_post

Hi,

i tried the code from the following post: https://theme.co/apex/forum/t/recent-posts-shortcode-change-image-size-add-excerpt/1517/2

but unfortunately the excerpt which is shown isn’t correct. It is correct for the first entry. For the second entry the the excerpt from the first is displayed and for the third entry the excerpt of the second post is displayed. Are you able to help me with that?

Thanks a lot!

Hello There,

Thanks for writing in. I was able to find your site and check your homepage. I’ve seen the recent post and it seems that the structure is not of the recent post element that we have. Could you please share your code? You may have modified the code and that may have been the caused of the issue.

If you could share the code in your next reply, that would really help us as we can also test code in our local testing server.

Regards.

Hi RueNel,

yes I restructured the code (just the positions and classes, nothing regarding the loop). With the original code it is absolutely the same problem. I tested it yesterday with the original code, as I was thinking I made a mistake in editing.

Best regards

Björn

If you still need the code please inform me.

Hi There,

Please provide us the code you’re using so we can take a closer look.

Thank you.

Hi Thai,

the following is my actual code:

function x_shortcode_recent_posts_v2code($atts)
{
    extract(shortcode_atts(array(
        'id' => '',
        'class' => '',
        'style' => '',
        'type' => '',
        'count' => '',
        'category' => '',
        'enable_excerpt' => '',
        'offset' => '',
        'orientation' => '',
        'no_image' => '',
        'fade' => ''
    ), $atts));

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

    $output = "<div {$id} class=\"entry-wrap {$class}{$orientation}\" {$style} data-fade=\"{$fade}\">";
    $q = new WP_Query(array(
        'orderby' => 'date',
        'post_type' => "{$type}",
        'posts_per_page' => "{$count}",
        'offset' => "{$offset}",
        "{$category_type}" => "{$category}",
        'post_status' => "publish",
    ));


    while ($q->have_posts()) {
        $q->the_post();

        if ($no_image == 'true') {
            $image_output = '';
            $image_output_class = 'no-image';
        } else {
            $image_output = '<div class="">' . get_the_post_thumbnail(get_the_ID(), 'medium', NULL) . '</div>';
            $image_output_class = 'with-image';
        }


        $output .= '<article id="post-' . get_the_ID() . '" class="' . implode(' ', get_post_class()) . '">'
                       . '<header class="entry-header">'
                          .  '<h2 style="font-size: 4em;" class="entry-title">'
                               . '<a class="" href="' . get_permalink(get_the_ID()) . '" title="' . esc_attr(sprintf(__('Permalink to: "%s"', '__x__'), the_title_attribute('echo=0'))) . '">'
                                    . get_the_title()
                                . '</a>'
                             . '</h2>'
                        . '</header>'
                        . '<div class="x-container">'
                           . '<div class="x-column x-sm x-1-2 entry-featured" style="width: 300px;">'
                               . '<a class="entry-thumb" href="' . get_permalink(get_the_ID()) . '" title="' . esc_attr(sprintf(__('Permalink to: "%s"', '__x__'), the_title_attribute('echo=0'))) . '">' . $image_output . '</a>'
                            . '</div>'
                            .'<div class="excerpt x-column x-sm x-1-2" style="margin-top: 25px;">' . get_the_excerpt() . '</div>'
                       . '</div>'
                    .'</article>';


    }

    wp_reset_postdata();

    $output .= '</div>';

    return $output;
}

And I tried the exact code from the link i posted with the exact same result regarding to the excerpt.

Thank you!

Hi,

You can try this code instead.

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

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

    endwhile; endif; wp_reset_postdata();

  $output .= '</div>';

  return $output;

}

add_filter('wp_head', 'custom_recent_posts');

function custom_recent_posts() {
  remove_shortcode( 'x_recent_posts' );
  remove_shortcode( 'recent_posts' );
  add_shortcode( 'x_recent_posts', 'x_shortcode_recent_posts_v2' );
  add_shortcode( 'recent_posts', 'x_shortcode_recent_posts_v2' );
}

If that doesn’t help, please provide us your wordpress admin login in Secure Note

Thanks

Hey @MrWhy,

I’m sorry but I’m unable to fix the code for your site but @paul.r’s code does not cause the issue you described in my test site. Though my colleagues attempted to add the excerpt, this usually opens several more customizations and issues because the Recent Post shortcode was not designed to have an excerpt. There might be future improvements to the Recent Post but for now, I’d recommend that you use The Grid or Essential Grid plugin to display your post listing. Both plugins come with an option to display an excerpt out of the box. For more details, please see the links below:

Also note that, the code provided in this forum serves only as a guide. Issues arising from the use of it and any enhancements should be handled by the user or a third party developer.

Thank you for understanding.

Hi Christian,

thank you for your efforts. I will have a look at the two Extensions.
Pauls code has exact the same problem in this installation like the code i posted.

Best regards.

You’re welcome, @MrWhy. There could be something in your site or page messing up with the loop. You could try testing for a plugin conflict. You can do this by deactivating all third party plugins, and seeing if the problem remains. If it’s fixed, you’ll know a plugin caused the problem, and you can narrow down which one by reactivating them one at a time. Also do this for other customizations if you have any.

If that does not work, regretfully, you will need to use either of the suggested plugins.

Thanks.

It is also not working with all plugins disabled. If I use the excerpt field in the backend it is rendered correctly. If I use “get_the_content” in the function, i get the content of the active site(with recursion) not the post content from the WP_Query loop.
Is it possible that it is a bug in Pro or Wordpress itself?
Best regards

Hi there,

It shouldn’t be the case since it works okay on my end. Perhaps you can provide your FTP login credentials in a secure note?

And based on your provided information, it seems that something is modifying get_the_content output.

Thanks!

@Rad, I left the var_dump in the child template function, that you can see the output i meant. I disabled all plugins except the slider (I also checked that, but it had no effect on the problem).

I also added a testing env now. Only Pro and child theme no extra plugins. There is no excerpt created with this. But the “get_the_content()” function acts like on the productive installation.

Unfortunately I’m not able to reopen this ticket again to thank you and post my final code. The hint with the do_shortcode function helped me to get my excerpt. For all who might be interessted, use this to get your excerpt:

wp_trim_words(do_shortcode( get_the_content() ) );

Hi there,

I checked and get_the_content() only displays shortcodes and you have to execute it by calling do_shortcode(). Please check the change I made.

Please note that calling that means it’s will display a full post content instead of excerpt.

Thanks!

1 Like

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