Imagine you have set your excerpt length in your themes function.php, like this:
function my_custom_excerpt_length($length)
{
return 25;
}
add_filter( 'excerpt_length', 'my_custom_excerpt_length', 999 );
According to the codex, the excerpt length is defined as "the number of words you wish to display in the excerpt:"
http://codex.wordpress.org/Plugin_API/Filter_Reference/excerpt_length
[wpv-post-excerpt] on the other hands, will treat this as the amount of characters to display. So the excerpt generated will be much much smaller.
I'm not sure why you have implemented your own excerpt generator, here's a drop-in replacement for your shortcode that works (although I haven't checked all edge cases):
function shortcode_get_the_excerpt($atts)
{
return get_the_excerpt();
}
add_shortcode( 'wpv-post-excerpt', 'shortcode_get_the_excerpt' );
Regardless, your shortcode should interpret excerpt_length as words and not characters. 25 words or 25 characters can make a big difference in length...
You can disregard the previous version of my shortcode, as it did not work in all cases.
Here is the improved version:
function shortcode_get_the_excerpt($atts)
{
extract(shortcode_atts(array('id' => null), $atts));
$trim_length = apply_filters('excerpt_length', 55);
if($id !== null)
{
$post = get_post(intval($id));
return $post->post_excerpt !== '' ? $post->post_excerpt : wp_trim_words( $post->post_content, $trim_length);
}
else
return __('ERROR: Invalid post ID given');
}
add_shortcode( 'get-the-excerpt', 'shortcode_get_the_excerpt' );
Usage in Content Template or View:
[get-the-excerpt id="[wpv-post-id]"]
Any workaround available so I don't have to pass [wpv-post-id] as an argument to the shortcode? (Is there a global variable that can be used instead?)
Dear Stanislav,
You can use advanced excerpts by creating your own shortcode. Add these lines to functions.php in your theme and you can use the [advanced-shortcode] in your View Templates:
add_shortcode('advanced-excerpt', 'my_advanced_excerpt');
function my_advanced_excerpt() {
return the_advanced_excerpt(get_the_excerpt(), true);
}
Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.
Regards,
Caridad
CardidadZ,
The problem is that Views incorrectly uses the excerpt_length filter in the [wpv-post-excerpt] shortcode.
excerpt_length is the amount of words the excerpt should be.
Example:
excerpt_length = 4
get_the_excerpt():
Here is an excerpt...
[wpv-post-excerpt]
Here...
Do you see the difference? The first example is 4 WORDS, the second is 4 CHARACTERS.
If we look inside wpv_shortcode_wpv_post_excerpt() function of Views:
...
$excerpt_length = apply_filters('excerpt_length', 252);
...
$excerpt = wp_html_excerpt($excerpt, $excerpt_length)...
...
But wp_html_excerpt takes amount of -characters- as second parameter, not words.
Instead of wp_html_excerpt, you should use wp_trim_words() so that your function is consistent with get_the_excerpt();
Or at least stop using the excerpt_length filter, because it makes it impossible to use the [wpv-post-excerpt] shortcode...
Does that make sense?
Dear Stanislav,
Let me ask the developers about this and I will get back to you when I have more information.
Regards,
Caridad
Dear Stanislav,
We are reviewing it and we will change it on the next version of Views. Keep an eye on the changelog to see what the change will be.
Thank you for bringing this up.
Regards,
Caridad