skip to Main Content

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


  1. Chosen as BEST ANSWER

    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

                        // do not display the content if it contains Blog, Post Slider, Fullwidth Post Slider, or Portfolio modules to avoid infinite loops
                        if ( ! has_shortcode( $post_content, 'et_pb_blog' ) && ! has_shortcode( $post_content, 'et_pb_portfolio' ) && ! has_shortcode( $post_content, 'et_pb_post_slider' ) && ! has_shortcode( $post_content, 'et_pb_fullwidth_post_slider' ) ) {
                            if ( 'on' === $show_content ) {
                                global $more;
    
                                // page builder doesn't support more tag, so display the_content() in case of post made with page builder
                                if ( et_pb_is_pagebuilder_used( get_the_ID() ) ) {
                                    $more = 1;
                                    the_content();
                                } else {
                                    $more = null;
                                    the_content( esc_html__( 'read more...', 'et_builder' ) );
                                }
                            } else {
                                if ( has_excerpt() ) {
                                    the_excerpt();
                                } else {
                                    if(in_category(20)) {
                                      echo wpautop( truncate_post( 70, false ) );
                               } else {
                              echo wpautop( truncate_post( 370, false ) );
                              }
    
                                }
                            }
                        } else if ( has_excerpt() ) {
                            the_excerpt();
                        }
    

  2. To override the post_excerpt lengh, you can find in custom_functions.php the function truncate_post()

    if ( ! function_exists( 'truncate_post' ) ) {
    
        function truncate_post( $amount, $echo = true, $post = '', $strip_shortcodes = false ) {
            global $shortname;
    
            if ( '' == $post ) global $post;
    
            $post_excerpt = '';
            $post_excerpt = apply_filters( 'the_excerpt', $post->post_excerpt );
    
            if ( 'on' == et_get_option( $shortname . '_use_excerpt' ) && '' != $post_excerpt ) {
                if ( $echo ) echo $post_excerpt;
                else return $post_excerpt;
            } else {
                // get the post content
                $truncate = $post->post_content;
    
                // remove caption shortcode from the post content
                $truncate = preg_replace( '@[caption[^]]*?].*?[/caption]@si', '', $truncate );
    
                // remove post nav shortcode from the post content
                $truncate = preg_replace( '@[et_pb_post_nav[^]]*?].*?[/et_pb_post_nav]@si', '', $truncate );
    
                // Remove audio shortcode from post content to prevent unwanted audio file on the excerpt
                // due to unparsed audio shortcode
                $truncate = preg_replace( '@[audio[^]]*?].*?[/audio]@si', '', $truncate );
    
                if ( $strip_shortcodes ) {
                    $truncate = et_strip_shortcodes( $truncate );
                } else {
                // apply content filters
                    $truncate = apply_filters( 'the_content', $truncate );
                }
    
                // decide if we need to append dots at the end of the string
                if ( strlen( $truncate ) <= $amount ) {
                    $echo_out = '';
                } else {
                    $echo_out = '...';
                    // $amount = $amount - 3;
                }
    
                // trim text to a certain number of characters, also remove spaces from the end of a string ( space counts as a character )
                $truncate = rtrim( et_wp_trim_words( $truncate, $amount, '' ) );
    
                // remove the last word to make sure we display all words correctly
                if ( '' != $echo_out ) {
                    $new_words_array = (array) explode( ' ', $truncate );
                    array_pop( $new_words_array );
    
                    $truncate = implode( ' ', $new_words_array );
    
                    // append dots to the end of the string
                    $truncate .= $echo_out;
                }
    
                if ( $echo ) echo $truncate;
                else return $truncate;
            };
        }
    
    
    }
    

    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.php

    If 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

    if(in_category(20)) {
        truncate_post( 30 );
    } else {
        truncate_post( 60 );
    }
    

    It can be easier

    Hope it helps

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search