lots0logs
4/15/2016 - 1:52 AM

WordPress :: Elegant Themes :: Custom Excerpt Length

WordPress :: Elegant Themes :: Custom Excerpt Length

<?php
/* DON'T copy the first line (above) if your functions.php already has it. 
 * ---------------------------------------------------------------------- */
 
 
function et_truncate_post( $amount, $echo = true, $post = '' ) {
	global $shortname;
	if ( '' == $post ) global $post;
	$post_excerpt = '';
	$post_excerpt = apply_filters( 'the_excerpt', $post->post_excerpt );
	
	
	/* ==================================================================
	 * >>>> Replace "230" below with your desired character length.  <<<< 
	 * ==================================================================
	 */ $my_amount = 230;
	
	
	if ( 'on' == et_get_option( $shortname . '_use_excerpt' ) && '' != $post_excerpt ) {
		if ( $echo ) echo $post_excerpt;
		else return $post_excerpt;
	} else {
		// get the post content
		$truncate = $post->post_content;
		// remove caption shortcode from the post content
		$truncate = preg_replace( '@\[caption[^\]]*?\].*?\[\/caption]@si', '', $truncate );
		// apply content filters
		$truncate = apply_filters( 'the_content', $truncate );
		// decide if we need to append dots at the end of the string
		if ( strlen( $truncate ) <= $my_amount ) {
			$echo_out = '';
		} else {
			$echo_out = '...';
			// $amount = $amount - 3;
		}
		// trim text to a certain number of characters, also remove spaces from the end of a string ( space counts as a character )
		if ( ! $echo ) {
			$truncate = rtrim( et_wp_trim_words( $truncate, $my_amount, '' ) );
		} else {
			$truncate = rtrim( wp_trim_words( $truncate, $my_amount, '' ) );
		}
		// remove the last word to make sure we display all words correctly
		if ( '' != $echo_out ) {
			$new_words_array = (array) explode( ' ', $truncate );
			array_pop( $new_words_array );
			$truncate = implode( ' ', $new_words_array );
			// append dots to the end of the string
			$truncate .= $echo_out;
		}
		if ( $echo ) echo $truncate;
		else return $truncate;
	};
}

?>