Ejemplo n.º 1
0
/**
 * Smart Excerpt
 *
 * Returns an excerpt which is not longer than the given length and always
 * ends with a complete sentence. If first sentence is longer than length,
 * it will add the standard ellipsis… Length is the number of characters.
 *
 * Usage: <?php smart_excerpt(450); >
 *
 * http://www.distractedbysquirrels.com/blog/wordpress-improved-dynamic-excerpt
 */
function smart_excerpt($length, $postId = false)
{
    if ($postId) {
        $post = get_post($postId);
        $text = $post->post_excerpt;
    } else {
        global $post;
        $text = $post->post_excerpt;
    }
    if ('' == $text) {
        if ($postId) {
            $text = $post->post_content;
        } else {
            $text = get_the_content('');
        }
        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
    }
    $text = strip_shortcodes($text);
    $text = wp_strip_all_tags($text, true);
    if (empty($length)) {
        $length = 300;
    }
    $text = substr($text, 0, $length);
    $excerpt = nucleus_reverse_strrchr($text, '.', 1);
    if ($excerpt) {
        echo apply_filters('the_excerpt', $excerpt);
    } else {
        echo apply_filters('the_excerpt', $text . '…');
    }
}
Ejemplo n.º 2
0
/**
 * Smart Excerpt
 *
 * Returns an excerpt which is not longer than the given length and always
 * ends with a complete sentence. If first sentence is longer than length,
 * it will add the standard ellipsis… Length is the number of characters.
 *
 * Usage: <?php smart_excerpt(450); >
 *
 * http://www.distractedbysquirrels.com/blog/wordpress-improved-dynamic-excerpt
 */
function smart_excerpt($length)
{
    global $post;
    $text = $post->post_excerpt;
    if ('' == $text) {
        $text = get_the_content('');
        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
    }
    $text = strip_shortcodes($text);
    $text = strip_tags($text);
    // use ' $text = strip_tags($text,'<p><a>'); ' if you need to keep some tags
    if (empty($length)) {
        $length = 300;
    }
    $text = substr($text, 0, $length);
    $excerpt = nucleus_reverse_strrchr($text, '.', 1);
    if ($excerpt) {
        echo apply_filters('the_excerpt', $excerpt);
    } else {
        echo apply_filters('the_excerpt', $text . '…');
    }
}