😂Excerpt full (ok)

https://wpshout.com/wordpress-post-excerpts/#gref

wp_trim_words

wp_trim_words(get_the_excerpt(), 15);

Hoặc

public function excerpt( $new_length = 15, $echo, $more_text = 'more') {
if(!is_admin()):
  add_filter( 'excerpt_more', $more_text, 999 );
  add_filter( 'excerpt_length', $new_length, 999 );
endif;
  if( $echo )
  the_excerpt();
  else
  return get_the_excerpt();
}

How to Change the Length of Your WordPress Excerpts with the excerpt_length Filter

function wpshout_longer_excerpts( $length ) {
	// Don't change anything inside /wp-admin/
	if ( is_admin() ) {
		return $length;
	}
	// Set excerpt length to 140 words
	return 140;
}
// "999" priority makes this run last of all the functions hooked to this filter, meaning it overrides them
add_filter( 'excerpt_length', 'wpshout_longer_excerpts', 999 );

How to Change the “Read More” Text of Your WordPress Excerpts with the excerpt_more Filter

function wpshout_change_and_link_excerpt( $more ) {
	if ( is_admin() ) {
		return $more;
	}

	// Change text, make it link, and return change
	return '&hellip; <a href="' . get_the_permalink() . '">More doggerel »</a>';
 }
 add_filter( 'excerpt_more', 'wpshout_change_and_link_excerpt', 999 );

How to Change the Text of a Post Excerpt with the get_the_excerpt Filter

function wpshout_make_excerpt_text_interesting( $excerpt ) {
	if ( is_admin() ) {
		return $excerpt;
	}

	$excerpt = str_replace( array('rain', 'wind', 'scanty flame of the lamps'), 'DINOSAURS', $excerpt );
	return $excerpt;
}
add_filter( 'get_the_excerpt', 'wpshout_make_excerpt_text_interesting', 999 );

How to Create One-Paragraph Excerpts with the wp_trim_excerpt Filter

function wpshout_excerpt( $text ) {
	if( is_admin() ) {
		return $text;
	}
	// Fetch the content with filters applied to get <p> tags
	$content = apply_filters( 'the_content', get_the_content() );
	
	// Stop after the first </p> tag
	$text = substr( $content, 0, strpos( $content, '</p>' ) + 4 );
	return $text;
}
// Leave priority at default of 10 to allow further filtering
add_filter( 'wp_trim_excerpt', 'wpshout_excerpt', 10, 1 );

How to Create Excerpts of a Specific Character Length (Rather than Word Length) with the wp_trim_excerpt Filter

function wpshout_twitter_length_excerpt( $text ) {
	if( is_admin() ) {
		return $text;
	}
	// Fetch the post content directly
	$text = get_the_content();
	// Clear out shortcodes
	$text = strip_shortcodes( $text );
	
	// Get the first 140 characteres
	$text = substr( $text, 0, 140 );

	// Add a read more tag
	$text .= '…';
	return $text;
}
// Leave priority at default of 10 to allow further filtering
add_filter( 'wp_trim_excerpt', 'wpshout_twitter_length_excerpt', 10, 1 );

How to Use wp_trim_words() to Get a WordPress Excerpt of Any Length from Arbitrary Text

// We're creating $read_more, a string that we'll place after the excerpt
$read_more = '&hellip; <a class="read-more-link" href="' . get_the_permalink() . '">Read Full Article</a>';

// wpautop() auto-wraps text in paragraphs
echo wpautop( 
	// wp_trim_words() gets the first X words from a text string
	wp_trim_words(
		get_the_content(), // We'll use the post's content as our text string
		55, // We want the first 55 words
		$read_more // This is what comes after the first 55 words
	)
);

Last updated