Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1319068
    Pbalazs89
    Participant

    Hi there,

    I’m trying to set up the ”recent posts” element on my website to display posts with their respective excerpts.

    The website:

    http://biokutatas.decorolux.com/

    I found this discussion here: https://community.theme.co/forums/topic/recent-post-excerpt-and-excerpt-length-cornerstone/#post-297951

    And added the following shortcode found there:

    
    function x_shortcode_recent_posts_v2code( $atts ) {
      extract( shortcode_atts( array(
        'id'          => '',
        'class'       => '',
        'style'       => '',
        'type'        => 'post',
        'count'       => '',
        'category'    => '',
        'offset'      => '',
        'orientation' => '',
        'no_image'    => '',
        'fade'        => '',
        'enable_excerpt' => ''
      ), $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';
      $no_image      = ( $no_image    == 'true'      ) ? $no_image : '';
      $fade          = ( $fade        == 'true'      ) ? $fade : 'false';
      $enable_excerpt = ( $enable_excerpt == 'true' ) ? true: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}"
        ) );
    
        if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post();
         $content_excerpt = get_the_excerpt();
    
          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';
          }
    
          $output .= '<a class="x-recent-post' . $count . ' ' . $image_output_class . '" href="' . get_permalink( get_the_ID() ) . '" title="' . esc_attr( sprintf( __( 'Permalink to: "%s"', csl18n() ), 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>'
                           . ( $enable_excerpt ? '<span class="x-recent-posts-excerpt">' . strip_tags($content_excerpt ) . '</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' );
    }
    
    // Excerpt Custom Length
    // =============================================================================
    function x_excerpt_length( $length ) {
        return 60;
    }  
    add_filter( 'excerpt_length', 'x_excerpt_length' );
    
    //

    To my X child theme’s function.php

    Unfortunately it breaks the site before I could do anything.
    Any idea why?

    #1319215
    Jade
    Moderator

    Hi there,

    Would you mind providing us with login credentials so we can take a closer look? To do this, you can make a post with the following info:

    – Link to your site
    – WordPress Admin username / password
    – FTP credentials

    Don’t forget to select Set as private reply. This ensures your information is only visible to our staff.

    #1319259
    Pbalazs89
    Participant
    This reply has been marked as private.
    #1319828
    Rue Nel
    Moderator

    Hello There,

    Thanks for providing the information. I have check your child theme’s functions.php file. The code is already there. It should have work on the page right now. So I went ahead and added a test page. You can find it here: http://biokutatas.decorolux.com/an-x-test/

    You should update your code and use this:

    function change_recent_posts_to_v2() {
    	remove_shortcode( 'x_recent_posts' );
    	add_shortcode( 'x_recent_posts', 'x_shortcode_recent_posts_v2code' );
    }

    Hope this helps. Please let us know how it goes.

    #1319930
    Pbalazs89
    Participant

    Hi, thanks for the help, however, when on the home page, there is some additional whitespace under each recent post, and the images don’t seem to cover the entire container. Any idea why that is?

    *managed to fix the issue with padding.

    Another question – is there a way to add a read more… button to the end of the excerpt?

    #1320007
    Christopher
    Moderator

    Hi there,

    Please update your code in child theme’s functions.php file to :

    function x_shortcode_recent_posts_v2code( $atts ) {
      extract( shortcode_atts( array(
        'id'          => '',
        'class'       => '',
        'style'       => '',
        'type'        => 'post',
        'count'       => '',
        'category'    => '',
        'offset'      => '',
        'orientation' => '',
        'no_image'    => '',
        'fade'        => '',
        'enable_excerpt' => ''
      ), $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';
      $no_image      = ( $no_image    == 'true'      ) ? $no_image : '';
      $fade          = ( $fade        == 'true'      ) ? $fade : 'false';
      $enable_excerpt = ( $enable_excerpt == 'true' ) ? true: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}"
        ) );
    
        if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post();
         $content_excerpt = get_the_excerpt();
    
          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';
          }
    
          $output .= '<a class="x-recent-post' . $count . ' ' . $image_output_class . '" href="' . get_permalink( get_the_ID() ) . '" title="' . esc_attr( sprintf( __( 'Permalink to: "%s"', csl18n() ), 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>'
                           . ( $enable_excerpt ? '<span class="x-recent-posts-excerpt">' . strip_tags($content_excerpt ) . '</span>' : '' )
                           .'<div>' . __( 'Read More', '__x__' ) .'</div>'
                         . '</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' );
    }
    
    // Excerpt Custom Length
    // =============================================================================
    function x_excerpt_length( $length ) {
        return 60;
    }  
    add_filter( 'excerpt_length', 'x_excerpt_length' );
    
    //

    Hope it helps.

    #1320063
    Pbalazs89
    Participant

    Hi there,

    Well, the ”read more” is there, so I guess I shouldn’t complain ^^, but unfortunately this new code made the excerpt and the featured image disappear. Any ideas?

    #1320567
    Rad
    Moderator

    Hi there,

    Please remove this existing custom CSS.

    .x-recent-posts .x-recent-posts-img {
        padding-bottom: 0px; 
    }

    Thanks!

    #1320868
    Pbalazs89
    Participant

    Hi Rad,

    Thanks, that fixed the issue with the images, but the excerpt is still not showing up.

    http://biokutatas.decorolux.com/

    #1320944
    Christopher
    Moderator

    Hi there,

    You should add recent posts shortcode in a text element, check this page : http://biokutatas.decorolux.com/?page_id=528&preview=true

    Hope it helps.

    #1321296
    Pbalazs89
    Participant

    Hi,

    Thank you for that, everything works fine.

    One last question. My site will be multilingual, and I’m wondering if there’s a way to translate the ”read more” string in the recent posts element to Hungarian on the Hungarian end of the site?

    #1321368
    Nico
    Moderator

    Hi There,

    Happy to hear that it worked fine.

    In regards to translation, please check the link below.

    https://community.theme.co/kb/translation/

    Hope it helps.

    Let us know how it goes.

    Thanks.

  • <script> jQuery(function($){ $("#no-reply-1319068 .bbp-template-notice, .bbp-no-topic .bbp-template-notice").removeClass('bbp-template-notice'); }); </script>