Home › Forums › Calendar Products › Events Calendar PRO › Changing excerpt length
- This topic has 8 replies, 2 voices, and was last updated 6 years, 2 months ago by Ole Kroer.
-
AuthorPosts
-
January 15, 2018 at 10:01 am #1426646Ole KroerParticipant
Hello,
We have a issue with changing the excerpt link. I found the following code on your support forum for allowing HTML characters in excerpts as we need them in the overview. However, this function broke our other function to set how many characters/words we want to show in the excerpts:
add_filter( 'wp_trim_words', 'tribe_preserve_html_in_excerpt', 10, 4 );
function tribe_preserve_html_in_excerpt( $text, $num_words, $more, $original_text ) {remove_filter( 'wp_trim_words', 'tribe_preserve_html_in_excerpt', 10 ); $excerpt = force_balance_tags( html_entity_decode( wp_trim_words( htmlentities( $original_text ) ) ) ); add_filter( 'wp_trim_words', 'tribe_preserve_html_in_excerpt', 10, 4 ); return $excerpt;
}
This is the code we used to control the excerpt length which worked fine before:
function custom_excerpt_length( $length ) {
return 30;
}add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
I hope you can help us out even though this might not be what you are used to help with.
Thank you VERY much in advance!January 16, 2018 at 9:46 am #1427734SkyKeymasterHi Ole!
I will try to help you get this sorted.
Not sure where the first bit of code came from, but it doesn’t look properly constructed. The code you are using to set the excerpt length looks fine, however.
There is a filter in the function that outputs the excerpt that determines which html is allowed inside. By default, the list is pretty short. However, you can override this by passing in an expanded list. WordPress uses something called “wp_kses” to detail which html elements and attributes can be used in various circumstances. The data has to be passed in using a certain format, in array form.
Here is an example of adding more html elements to be allowed in the excerpts by using this filter. If you need more information on formatting for wp_kses, check out the WordPress documentation: https://codex.wordpress.org/Function_Reference/wp_kses
Let me know if that works for you.
Thanks,
SkyJanuary 17, 2018 at 10:57 am #1428729Ole KroerParticipantHi Sky,
Thanks a lot for your reply!
I don’t know if I have understood your reply correctly, but I’ve tried to comment the previous code out and insert yours but the excerpts doesn’t show any HTML formatting. Am I doing something wrong or have I simply just misunderstood your reply?
I’m looking forward to hearing from you!
January 18, 2018 at 10:56 am #1429698SkyKeymasterHi Ole,
If you paste this code: https://gist.github.com/skyshab/7112ceb63bf6641faee4aa28527ed3e1 into your functions.php in place of the code you pasted above, it will allow you to use all those elements in your excerpts. I have tested it on a local install to make sure it worked.
You will still need the code you have for the excerpt length, which looks fine.
Just to clarify, where are you adding this code? Are you using a child theme?
Thanks,
SkyJanuary 20, 2018 at 3:14 am #1431272Ole KroerParticipantHello,
Thanks for the clarification. We are using a child theme, but the other functions are working fine. I have copied the functions.php file and pasted it in here: https://gist.github.com/snaever/ff7de3def828c9651d80177e1e4c48c0
Thanks a lot for your time!
January 20, 2018 at 3:22 am #1431275Ole KroerParticipantI have added two screenshots below. Screenshot 1 shows how it looks with your code and screenshot 2 shows how it looks with the old code, but without us being able to control the text length.
January 22, 2018 at 10:57 am #1432625SkyKeymasterOle,
I have more understanding of the issue now. The code I provided will increase the number of HTML elements allowed in the manual excerpt field. However, excerpts that are generated by WordPress from the content get run through “wp_trim_words” which strips out all of the tags. Right now, we do not have a workaround for this. In order to allow HTML in your excerpts, you will have to manually create them in each event. This will ignore the auto generated ‘excerpt_size’ filter however, so you will need to enter them exactly the length you want them to display.
You can read more information about this issue here: https://theeventscalendar.com/the-trouble-with-excerpts/
I’m not sure where you found the original snippet which attempted to work around the issue, but as it is not an official function or snippet, we unfortunately cannot provide support for it.
I’m sorry I didn’t have a better solution to offer up. Please let us know if you have any other issues in the future.
Thanks,
SkyFebruary 13, 2018 at 8:37 am #1451804Support DroidKeymasterHey there! This thread has been pretty quiet for the last three weeks, so we’re going to go ahead and close it to avoid confusion with other topics. If you’re still looking for help with this, please do open a new thread, reference this one and we’d be more than happy to continue the conversation over there.
Thanks so much!
The Events Calendar Support Team -
AuthorPosts
- The topic ‘Changing excerpt length’ is closed to new replies.