Skip to content

Instantly share code, notes, and snippets.

@marcwiest
Last active April 13, 2020 05:37
Show Gist options
  • Select an option

  • Save marcwiest/7114910 to your computer and use it in GitHub Desktop.

Select an option

Save marcwiest/7114910 to your computer and use it in GitHub Desktop.
Better WordPress Post–Page / Content–Excerpt Function
<?php
function better_wp_post_content( $word_limit=false, $force_excerpt=false, $do_shortcode=false ) {
$post = get_post();
$excerpt_metabox = $post->post_excerpt;
// If word limit was used, return the excerpt.
if ( is_int( $word_limit ) ) :
$excerpt = $force_excerpt ? $excerpt_metabox : get_the_excerpt();
$excerpt = explode( ' ', $excerpt );
$excerpt = implode( ' ', array_slice( $excerpt, 0, $word_limit ) );
$return = $excerpt;
endif;
// If word limit is FALSE, return the content.
if ( $word_limit == false )
$return = $force_excerpt ? $excerpt_metabox : get_the_content();
// Activate $do_shortcode if it's get_the_content().
if ( $word_limit == false && $force_excerpt == false )
$do_shortcode = true;
// Check $do_shortcode.
if ( $do_shortcode )
$return = do_shortcode( $return );
return wpautop( $return );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment