Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1138113
    PhilTone
    Participant

    Hey guys,

    I am having trouble to show an excerpt for the recent posts correctly.
    I would like to show the excerpt including HTML tags so i have can have paragraphs etc.

    What i have in the functions.php right now to show the excerpt is this:

    // Recent Posts 
    
    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;
    }
    
    // =============================================================================
    // Custom Excerpt Length - Call: echo excerpt($length);
    // =============================================================================
    
    // =============================================================================
    // Add Excerpt to Recent Posts
    // =============================================================================
    
    function x_shortcode_recent_posts_v2code( $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>'
                           . '<span class="x-recent-posts-excerpt">' .  strip_tags( excerpt(220) )  . '</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' );
    }
    
    function improved_trim_excerpt($text) { // Fakes an excerpt if needed
      
      global $post; 
      if ( '' == $text ) {
        $text = strip_tags($text, '<p>,<br>'); 
        $text = get_the_content('');
        $text = apply_filters('the_content', $text);
        $text = str_replace('\]\]\>', ']]>', $text);
    
        $excerpt_length = 230;
        $words = explode(' ', $text, $excerpt_length + 1);
        if (count($words)> $excerpt_length) {
          array_pop($words);
          array_push($words, ' &nbsp;[...]');
          $text = implode(' ', $words);
        }
      }
    return $text; 
    }
    
    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'improved_trim_excerpt');

    Now this doesn’t give me the ability to show paragraphs etc.

    I tried adding the plugin “Advanced Excerpt” wich is said to make it possible to show the HTML tags but activating it doesn’t work.

    Could you help me out here?

    Is there any workaround to somehow get this working?

    Thx in advance,
    Phil

    #1138116
    PhilTone
    Participant
    This reply has been marked as private.
    #1138165
    Paul R
    Moderator

    Hi Phil,

    Please replace this line of code

    
       . '<span class="x-recent-posts-excerpt">' .  strip_tags( excerpt(220) )  . '</span>'
    

    with this

    
       . '<span class="x-recent-posts-excerpt">' .  preg_replace('/<a.*?more-link.*?<\/a>/', '', cs_get_raw_excerpt() )  . '</span>'
    

    then enable excerpt in your post and add your own excerpt manually.

    http://screencast.com/t/LGkseq9fCv

    Hope that helps.

    #1139273
    PhilTone
    Participant

    hey,

    thx – now the manual excerpt works but actually I would like to have the recent post show an excerpt automatically while that showing the HTML tags like paragraphs etc so i don’t just have one big block of text if the post is longer.

    Right now my customer would have to manually place the excerpt and also add line breaks and paragraphs manually with HTML code wich he is not capable of ..

    Is that possible?

    #1139279
    Christopher
    Moderator

    Hi there,

    Wordpress will strip out all HTML and shortcode to generate plain text excerpt. Thus, if you compose your post content with shortcode, cornerstone or VC, then your content will be strip out along with the shortcode. Builder is mainly for layout and structure which is best for pages, but not for articles like posts. Though, you can always use manual excerpt.

    Thanks.

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