Navigation
This is archived content. Visit our new forum.
  • Author
    Posts
  • #371408

    Zeshan
    Member

    Hi there,

    There is currently no option to enable excerpt in Cornerstone. Though, we’ve forwarded this feature request to our developers for future consideration.

    In the meantime, if you want to enable excerpts by default in recent posts, you can use the following code instead of the provided one in your child theme’s functions.php file:

    // =============================================================================
    // Custom Excerpt Length - Call: echo excerpt($length);
    // =============================================================================
    
    function excerpt($limit) {
       $excerpt = explode(' ', get_the_excerpt(), $limit);
       if (count($excerpt)>=$limit) {
          array_pop($excerpt);
          $excerpt = implode(" ",$excerpt).'.';
       } else {
          $excerpt = implode(" ",$excerpt);
       } 
       // $excerpt = preg_replace('<code>\[[^\]]*\]</code>','',$excerpt);
       return $excerpt;
    }
    
    // =============================================================================
    // Add Excerpt to Recent Posts -> [x_recent_posts]
    // =============================================================================
    
    function x_shortcode_recent_posts_excerpt( $atts ) {
      extract( shortcode_atts( array(
        'id'          => '',
        'class'       => '',
        'style'       => '',
        'type'        => 'post',
        'count'       => '',
        'category'    => '',
        'offset'      => '',
        'orientation' => '',
        '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';
      $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' => true
        ) );
    
        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';
          }
    
          $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>'
                           . do_shortcode('[x_gap size="10px"]')
                           . '<span class="x-recent-posts-excerpt">' .  strip_tags(excerpt(25))  . '</span>'
                         . '</div>'
                       . '</div>'
                     . '</article>'
                   . '</a>';
    
        endwhile; endif; wp_reset_postdata();
    
      $output .= '</div>';
    
      return $output;
    }
    
    add_action('wp_head', 'update_x_recent_posts_to_v2');
    
    function update_x_recent_posts_to_v2() {
      remove_shortcode( 'x_recent_posts' );
      add_shortcode( 'x_recent_posts', 'x_shortcode_recent_posts_excerpt' );
    }
    

    Thanks!

    #371410

    rabbler
    Participant

    Great thank you, what exactly do I do with that? Just stick it into the function.php thing?

    #371412

    rabbler
    Participant

    Just to clarify, this is my site

    http://www.rabble.ie/

    If I stick in that code at the bottom of my functions.php

    Will it enable post excerpts under the recent posts sections

    eg the latest bits in prin

    and

    eg

    the latest blog post parts of the site?

    Thanks!!

    #371687

    Friech
    Moderator

    Hi There,

    Yes, it should. Paste the code on your child themeโ€™s functions.php file. Just below the:

    // Additional Functions
    // =============================================================================

    Hope it helps, Cheers!

    #372116

    rabbler
    Participant

    Hi guys

    I stuck that in there as per the instructions but no dice.

    Am I missing another setting?

    All I’m looking for is a few lines of excerpt below the various sections on the front.

    I love the new website, its stunning so thank you for all your help and a great theme.

    #372150

    Zeshan
    Member

    Hi Rabbler,

    Upon checking, I’m not seeing any child theme activated in your site. Please activate it under Settings > Themes. Once it is activated, you should be able to see the appropriate changes.

    Thank you for your kind words! ๐Ÿ™‚ And let us know how it goes.

    Cheers!

    #372178

    rabbler
    Participant

    What?! Sorry, I thought that was active all along. I just activated the child theme and it appears to have broken the site.

    What do I do now?? http://rabble.ie

    ๐Ÿ™

    #372269

    rabbler
    Participant

    Fixed this by deleting child theme via FTP. Ill work on the excerpts later. Thanks again.

    #372284

    rabbler
    Participant

    Child theme activated again and all working fine, that code was stuck into functions.php

    Still can’t see any excerpts on the front page though?

    #372331

    Zeshan
    Member

    Hi Rabbler,

    I’ve tested the code in my local setup and it is working just fine. To assist you better with this, kindly open a separate thread and provide us with your WordPress admin and FTP login credentials in a private reply (you can create a new reply to your new thread for the logins) so that we can take a closer look to the issue.

    Thanks!

    #372383

    psylist
    Participant

    Hi I’m trying to achieve the same thing…. I’m using ethos stack child theme and would like to have excerpt on my recent posts under the image title and the post’s date… But when I paste the code in the function.php I receive this error:
    Fatal error: Call to undefined function x_generate_data_attributes() in /home/psylist/public_html/deewan.ca/wp-content/themes/x-child/functions.php on line 97

    What do I do wrong?

    Thanks!

    #372683

    Rue Nel
    Moderator

    Hello There,

    Thanks for updating this thread. The error occurred because you are using an older version of the recent post shortcode. Please replace this line:
    $data = x_generate_data_attributes( 'recent_posts', $js_params );

    with this line instead:
    $data = cs_generate_data_attributes( 'recent_posts', $js_params );

    This should remove the error in your child theme’s functions.php file.

    Please let us know how it goes.

    #378747

    PhilTone
    Participant

    Is it possible to have the exceprt use “<br>” and “<p>” somehow?

    #378770

    Zeshan
    Member

    Hi there,

    This is not possible as excerpts remove any tags, script or shortcodes from the text and display it as a normal text. You can find more information regarding excerpts in WordPress codex: https://codex.wordpress.org/Excerpt.

    Though, it could be possible with custom development, but this would be outside the scope of support we can offer. You may wish to consult a developer to assist you with this. X is quite extensible with child themes, so there are plenty of possibilities.

    Thanks for understanding. Take care!

    #379121

    tlauen
    Participant

    Hi!

    I tried adding the code a few posts above the my functions.php in my child theme, but no excerpts shows up. I’m using the Renew stack.