Skip to content

Instantly share code, notes, and snippets.

@mcsf
Last active December 16, 2015 18:50
Show Gist options
  • Save mcsf/5481130 to your computer and use it in GitHub Desktop.
Save mcsf/5481130 to your computer and use it in GitHub Desktop.
[WordPress] Displays up to $length characters of the contents of the current post, taking care not to split words.
<?php
function mcsf_the_content( $length ) {
$content = get_the_content();
$content = apply_filters( 'the_content', $content );
$content = wp_filter_nohtml_kses( $content );
$extra = substr( $content, $length, $length + 20 );
$content = substr( $content, 0, $length );
$matches = array();
echo $content;
if ( preg_match( '/^( *)([^ ]+)/', $extra, $matches ) )
echo $matches[1] . $matches[2] . esc_html( ' [&hellip;]' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment