-
-
Save netzgestaltung/aa4aafa964c2928b2b04 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() { | |
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() { | |
$defaults = array( | |
'post_id' => NULL, | |
'limit' => 7, | |
'cut' => '...' | |
); | |
// Settings section | |
if ( isset($arguments) && !empty($arguments) ) { | |
if ( is_array($arguments) ) { | |
// we get an array of arguments | |
$settings = array_merge($defaults, $arguments[0]); | |
} else { | |
// apply default settings | |
$settings = $defaults; | |
$arguments = func_get_args(); | |
// apply single arguments | |
foreach ( $args as $key => $value ) { | |
if ( is_int($value) ) { | |
$settings['post_id'] = $value; | |
} | |
if ( is_string($value) ) { | |
$settings['cut'] = $value; | |
} | |
} | |
} | |
} | |
// 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
your code: _the_excerpt_of_cotent( null, 7, '...', false );
my code: _the_excerpt_of_cotent( false ); // i don not use $cut param
but you brought me to the idea to provide an array parameter to change the varibles and to not make use of func_get_args().
on line #16 you have to use do_shortcode, otherwise your content would not render the shortcodes. and your strip html function will not work for shortcodes only for html-tags. so therfore you translate first the shortcode to html to strip it and cut of the raw text without shortcodes and html.