Skip to content

Instantly share code, notes, and snippets.

@frontend-coder
Created July 24, 2022 19:20
Show Gist options
  • Save frontend-coder/e0006bb41b9c9330252ad0ce8c008933 to your computer and use it in GitHub Desktop.
Save frontend-coder/e0006bb41b9c9330252ad0ce8c008933 to your computer and use it in GitHub Desktop.
43. Кастомний exerpt #wordpress
function domain_trim_excerpt($length) {
global $post;
$explicit_excerpt = $post->post_excerpt;
if ( '' == $explicit_excerpt ) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
}
else {
$text = apply_filters('the_content', $explicit_excerpt);
}
$text = strip_shortcodes( $text ); // optional
$text = strip_tags($text);
$excerpt_length = $length;
$words = explode(' ', $text, $excerpt_length + 1);
if (count($words)> $excerpt_length) {
array_pop($words);
array_push($words, '[…]');
$text = implode(' ', $words);
$text = apply_filters('the_excerpt',$text);
}
return $text;
}
<?php echo wp_kses_post( domain_trim_excerpt(25) ); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment