Forked from facelordgists/wp-excerpt-outside-loop.php
Created
January 13, 2017 19:45
-
-
Save jongacnik/0fb8a51f368f32120a1c7db0cc6f1724 to your computer and use it in GitHub Desktop.
WORDPRESS: get excerpt outside loop
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
<? | |
/* works pretty well */ | |
function the_excerpt_max_charlength($id=false, $charlength=55) { | |
global $post; | |
$old_post = $post; | |
if ($id != $post->ID) { | |
$post = get_page($id); | |
} | |
if (!$excerpt = trim($post->post_excerpt)) { | |
//echo "<pre>" . print_r($post) . "</pre>"; | |
$excerpt = $post->post_content; | |
$excerpt = strip_shortcodes( $excerpt ); | |
$excerpt = apply_filters('the_content', $excerpt); | |
$excerpt = str_replace(']]>', ']]>', $excerpt); | |
$excerpt = strip_tags($excerpt); | |
function set_new_excerpt_length($length) { | |
return $charlength; | |
} | |
add_filter('excerpt_length', 'set_new_excerpt_length'); | |
$excerpt_length = apply_filters('excerpt_length', $charlength); | |
$words = preg_split("/[\n\r\t ]+/", $excerpt, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY); | |
if ( count($words) > $excerpt_length ) { | |
array_pop($words); | |
$excerpt = implode(' ', $words); | |
$excerpt = $excerpt . $excerpt_more; | |
} else { | |
$excerpt = implode(' ', $words); | |
} | |
} | |
$post = $old_post; | |
return $excerpt; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment