I have a category called ‘News’. The category ID is ’20’. I am using the Divi (Divi child) theme + WordPress and want to shorten the excerpt for the News category.
I typically would use the ‘add_filter’ function like this:
<pre>
add_filter('excerpt_length', 'news_excerpt_length');
function news_excerpt_length($length) {
if(in_category(20)) {
return 30;
} else {
return 60;
}
}
</pre>
But that ain’t workin’. I found the excerpt control in the ‘main-modules.php’ and figure to add my filter here? Has anyone done this?
I added the ‘main-module.php’ to the root of my child theme and then added this to my child ‘functions.php’
<pre>
if ( ! function_exists( 'et_builder_add_main_elements' ) ) :
function et_builder_add_main_elements() {
require ET_BUILDER_DIR . 'main-structure-elements.php';
require 'main-modules.php';
do_action( 'et_builder_ready' );
}
endif;
</pre>
It didn’t break the theme, but it didn’t work either. Does anyone have any experience with this particular issue?
-Thanks!
2
Answers
I ended up doing it ( although I don't know which solution is better ) by putting this in my 'main-module.php' starting on line 12319
To override the post_excerpt lengh, you can find in
custom_functions.php
the functiontruncate_post()
You don’t need to put
if ( ! function_exists( 'truncate_post' ) ) {
to make it work just create your function with the same name in functions.phpIf you are using a child theme (I really hope so with a theme like this), copy/paste index.php and paste these lines, line 54
It can be easier
Hope it helps