• Resolved vishypat

    (@vishypat)


    Hi,

    I am trying to customize my excerpts by using the following code in my Child theme’s function.php
    function custom_excerpt_length( $length ) {
    return 10;
    }
    add_filter( ‘excerpt_length’, ‘custom_excerpt_length’);

    function new_excerpt_more( $more ) {
    return ‘ Read More‘;
    }
    add_filter( ‘excerpt_more’, ‘new_excerpt_more’ );

    But this is not working. Is the theme’s custom_excerpt functionality interfering with this?

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi vishypat,

    yes – there is already an function for this.

    Btw. there is an option to set the custom excerpt length, please navigate to “Appearance => Customize => General Options” in your WordPress dashboard and just set the excerpt length there – that’s it.

    For the custom read more link, please add this function to the functions.php of your child theme:

    if (!function_exists('custom_excerpt')) {
    	function custom_excerpt($text = '') {
    		$raw_excerpt = $text;
    		if ('' == $text) {
    			global $post;
    			$options = get_option('mh_options');
    			$custom_length = empty($options['excerpt_length']) ? '35' : $options['excerpt_length'];
    			$text = get_the_content('');
    			$text = do_shortcode($text);
    			$text = apply_filters('the_content', $text);
    			$text = str_replace(']]>', ']]>', $text);
    			$excerpt_length = apply_filters('excerpt_length', $custom_length);
    			$excerpt_more = apply_filters('excerpt_more', ' ...');
    			$text = wp_trim_words($text, $excerpt_length, $excerpt_more);
    		}
    		return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
    	}
    	remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    	add_filter('get_the_excerpt', 'custom_excerpt');
    }

    And then just modify:

    $excerpt_more = apply_filters('excerpt_more', ' ...');

    Regards
    Michael

    Thread Starter vishypat

    (@vishypat)

    Seems like the above code will not work for manual excerpt. Was able to use your function custom_excerpt to achieve what I needed.

    Thanks

    Ok – looks like we posted at the same time… ??

    As I said, you can use the above function to solve your issue.

    Regards
    Michael

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘custom_excerpt_length not working’ is closed to new replies.