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

    Kevin L
    Participant

    Hello,

    Currently, the blog index shows the excerpts of the posts with the formatting tags stripped. Is there an easy way to keep the formatting instead?

    Cheers,
    Kevin.

    #101032

    Darshana
    Moderator

    Hi Kevin,

    You can do that by copying the following code to your functions.php file which is in the root of your theme folder. Before doing so, we’re highly recommending for you to create a child theme setup for your website so that your custom code will be remain separately.

    Please check our child theme setup guide at (http://theme.co/x/member/kb/how-to-setup-child-themes/).

    Once you activate the Child Theme, open the functions.php and add the following code.

    
    function custom_wp_trim_excerpt($text) {
    	$raw_excerpt = $text;
    	if ( '' == $text ) {
    	    //Retrieve the post content. 
    	    $text = get_the_content('');
    	 	$text = strip_shortcodes( $text );
    	    $text = apply_filters('the_content', $text);
    	    $text = str_replace(']]>', ']]>', $text);
    
    	    // the code below sets the excerpt length to 55 words. You can adjust this number for your own blog.
    	    $excerpt_length = apply_filters('excerpt_length', 55);
    
    	    // the code below sets what appears at the end of the excerpt, in this case ...
    	    $excerpt_more = apply_filters('excerpt_more', ' ' . '...');
    
    	    $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
    	    if ( count($words) > $excerpt_length ) {
    	        array_pop($words);
    	        $text = implode(' ', $words);
    	        $text = force_balance_tags( $text );
    	        $text = $text . $excerpt_more;
    	    } else {
    	        $text = implode(' ', $words);
    	    }
    
    	}
    	return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
    }
    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'custom_wp_trim_excerpt');
    

    Hope that helps.

    #102490

    Kevin L
    Participant

    Thanks! You guys are awesome!

    #102530

    Paul R
    Moderator

    You’re welcome Kevin. 🙂

    #114155

    Shannon M
    Participant

    I tried this, but the excerpt length doesn’t appear to be working. I’m in the Integrity stack. Any ideas? Thank you.

    #114165

    Shannon M
    Participant

    DISREGARD. I’m just a ditz. I had the “full” version selected in my WP reading settings. Switched to excerpt and it’s perfect. Sorry!

    #114270

    Rad
    Moderator

    Okay cool! No problem 🙂 Cheers.

    #812948

    edwinfu
    Participant

    I used the code in my child theme which is fantastic. However, there is a line break before the ‘… Read More’ that I want to remove. Is there some way to do so?

    Thank you

    #813117

    Rue Nel
    Moderator

    Hello Edwin,

    Thanks for updating this thread! To assist you better with this issue, would you mind providing us the url of your site with login credentials so we can take a closer look? This is to ensure that we can provide you with a tailored answer to your situation. Once you have provided us with your URL, we will be happy to assist you with everything.

    To do this, you can make a post with the following info:
    – Link to your site

    – WordPress Admin username / password

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

    Thank you.

    #814039

    edwinfu
    Participant
    This reply has been marked as private.
    #814486

    Christopher
    Moderator

    Hi there,

    Please update your code to :

    function custom_wp_trim_excerpt($text) {
    	$raw_excerpt = $text;
    	if ( '' == $text ) {
    	    //Retrieve the post content. 
    	    $text = get_the_content('');
    	 	$text = strip_shortcodes( $text );
    	    $text = apply_filters('the_content', $text);
    	    $text = str_replace(']]>', ']]>', $text);
    
    	    // the code below sets the excerpt length to 55 words. You can adjust this number for your own blog.
    	    $excerpt_length = apply_filters('excerpt_length', 55);
    
    	    // the code below sets what appears at the end of the excerpt, in this case ...
    	    $excerpt_more = apply_filters('excerpt_more', ' ' . ' ');
    
    	    $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
    	    if ( count($words) > $excerpt_length ) {
    	        array_pop($words);
    	        $text = implode(' ', $words);
    	        $text = force_balance_tags( $text );
    	        $text = $text . $excerpt_more;
    	    } else {
    	        $text = implode(' ', $words);
    	    }
    
    	}
    	return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
    }
    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'custom_wp_trim_excerpt');
    Hope that helps.

    Hope it helps.

    #814957

    edwinfu
    Participant

    It didn’t work. Unfortunately, it didn’t seem like there was any change at all.

    I’m looking for a solution that will remove the line break before the ‘…’. It should look as follows:

    <category page post>…
    <read more>

    #815299

    Rad
    Moderator

    Hi there,

    Please try this one,

    function custom_wp_trim_excerpt($text) {
    	$raw_excerpt = $text;
    	if ( '' == $text ) {
    	    //Retrieve the post content. 
    	    $text = get_the_content('');
    	 	$text = strip_shortcodes( $text );
    	    $text = apply_filters('the_content', $text);
    	    $text = str_replace(']]>', ']]>', $text);
    
    	    // the code below sets the excerpt length to 55 words. You can adjust this number for your own blog.
    	    $excerpt_length = apply_filters('excerpt_length', 55);
    
    	    // the code below sets what appears at the end of the excerpt, in this case ...
    	    $excerpt_more = '';
    
    	    $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
    	    if ( count($words) > $excerpt_length ) {
    	        array_pop($words);
    	        $text = implode(' ', $words);
    	        $text = force_balance_tags( $text );
    	        $text = $text . $excerpt_more;
    	    } else {
    	        $text = implode(' ', $words);
    	    }
    
    	}
    	return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
    }
    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'custom_wp_trim_excerpt');

    Completely removing the excerpt more dots.

    Please clear your caches before testing.

    Cheers!

    #815997

    edwinfu
    Participant

    I don’t want to completely remove the dots. I want to include the dots so that they appear without a line break after the cut-off at the excerpt.

    #816169

    Rue Nel
    Moderator

    Hello There,

    Do you want something like this?

    Please remove the code you have added and use this code instead:

    // Custom Read More string
    // =============================================================================
      function x_excerpt_string( $more ) {
        
        $stack = x_get_stack();
    
        if ( $stack == 'integrity' ) {
          return ' ... <a href="' . get_permalink() . '" class="more-link">' . __( 'Read More', '__x__' ) . '</a>';
        } else if ( $stack == 'renew' ) {
          return ' ... <a href="' . get_permalink() . '" class="more-link">' . __( 'Read More', '__x__' ) . '</a>';
        } else if ( $stack == 'icon' ) {
          return ' ...';
        } else if ( $stack == 'ethos' ) {
          return ' ...';
        }
    
      }
      add_filter( 'excerpt_more', 'x_excerpt_string' );
      // =============================================================================

    We would loved to know if this has work for you. Thank you.