Last active
August 29, 2015 14:02
-
-
Save webfacer/f8d428f14dabdb5f3dd6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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