Skip to content

Instantly share code, notes, and snippets.

@webfacer
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save webfacer/f8d428f14dabdb5f3dd6 to your computer and use it in GitHub Desktop.
Save webfacer/f8d428f14dabdb5f3dd6 to your computer and use it in GitHub Desktop.
<?php
/**
* @description Echoes custom the_excerpt from any post id. the_excerpt is 7 words long as default
*
* @arguments mixed
* @param array $arguments default NULL array of arguments - the below mentioned + $limit
* @param int $post_id default NULL the id of the post, if null, the current post is taken
* @param string $cut default '...' when nothing setted it will take the current Postcontent from loop and excerpt it
*/
function sandbox_the_excerpt( $arguments = array() ) {
echo sandbox_get_the_excerpt($arguments);
}
/**
* @description Returns custom the_excerpt from any post id. the_excerpt is 7 words long as default
*
* @arguments mixed
* @param array $arguments default NULL array of arguments - the below mentioned + $limit
* @param int $post_id default NULL the id of the post, if null, the current post is taken
* @param string $cut default '...' when nothing setted it will take the current Postcontent from loop and excerpt it
*
* @return string $the_excerpt
*/
function sandbox_get_the_excerpt( $arguments = array() ) {
$defaults = array(
'post_id' => NULL,
'limit' => 7,
'cut' => '...'
);
// Settings section
if ( isset($arguments) ) {
if ( is_array($arguments) && !empty($arguments) ) {
// we get an array of arguments
$settings = array_merge($defaults, $arguments);
} else {
// apply default settings
$settings = $defaults;
// clean up unused variables
unset( $defaults );
}
}
// get the post
$post = get_post($settings['post_id']);
// we need just the content
$content = trim($post->post_content);
// Apply Shortcodes
$content = trim(do_shortcode($content));
// prepare for word count
$plain_content = wp_strip_all_tags($content, true);
$before_the_excerpt = explode(' ', $plain_content, $settings['limit']);
if ( count($before_the_excerpt) >= $settings['limit'] ) {
array_pop($before_the_excerpt);
}
$the_excerpt = implode(' ', $before_the_excerpt) . $settings['cut'];
return $the_excerpt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment