• Resolved willzenstein

    (@willzenstein)


    How can I adjust the length of the excerpt shown at blog previews?

    The default WP way by adding the following custom function to the (child) theme does not show any success.

    function custom_excerpt_length( $length ) {
    	return 20;
    }
    add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

    Neither do any adjustments or changes to the theme function in inc/extras.php show any results:

    function accesspresslite_excerpt( $accesspresslite_content , $accesspresslite_letter_count ){
    		$accesspresslite_striped_content = strip_shortcodes($accesspresslite_content);
    		$accesspresslite_striped_content = strip_tags($accesspresslite_striped_content);
    		$accesspresslite_excerpt = mb_substr($accesspresslite_striped_content, 0, $accesspresslite_letter_count );
    		if($accesspresslite_striped_content > $accesspresslite_excerpt){
    			$accesspresslite_excerpt .= "...";
    		}
    		#return $accesspresslite_excerpt;
    		return 'test';
    	}

    I actually want to set a certain amount of words instead of characters. How could I achieve that?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter willzenstein

    (@willzenstein)

    Sorry, changing the function accesspresslite_excerpt() actually shows a result, but how could I adjust via a child theme so that I can still get the updates?

    Thread Starter willzenstein

    (@willzenstein)

    I found a way. The length of an excerpt is set by function accesspresslite_excerpt(). The second parameter is the length of the excerpt. You can find it in the templates. However, the function does simply cut off after the amount of given characters although it might be in the middle of a word. To cut off after a word you could do the following.

    Create a file called functions.custom.inc.php in your child theme directory and place the following custom function in it :

    function custom_excerpt( $accesspresslite_content , $accesspresslite_letter_count ){
    
    		$accesspresslite_striped_content = strip_shortcodes($accesspresslite_content);
    		$accesspresslite_striped_content = strip_tags($accesspresslite_striped_content);
    
    		$string = $accesspresslite_striped_content;
    		$your_desired_width = $accesspresslite_letter_count;
    		$parts = preg_split('/([\s\n\r]+)/', $string, null, PREG_SPLIT_DELIM_CAPTURE);
    		$parts_count = count($parts);
    
    		$length = 0;
    		$last_part = 0;
    		for (; $last_part < $parts_count; ++$last_part) {
    			$length += strlen($parts[$last_part]);
    			if ($length > $your_desired_width) { break; }
    		}
    
    		$excerpt = implode(array_slice($parts, 0, $last_part));
    		if( strlen($accesspresslite_striped_content) > $your_desired_width){
    			$excerpt .= " ...";
    		}
    		return $excerpt;
    	}

    Include it in index-one.php, index.php or any other template file where you should need it via
    require_once('functions.custom.inc.php');

    Replace all spots in the template files where accesspresslite_excerpt() is used with custom_excerpt()

    Dear willzenstein,

    Thank you for sharing. I’m wondering if I can use the below code for Accesspress Basic? I’m an newbie to WordPress. Your help is highly appreciated!

    Thank you in advance!

    Thread Starter willzenstein

    (@willzenstein)

    Hi Artiss,

    you can use the code if your theme or plugin is open source. If it is not you can use it too but it is really bad for your Karma ??

    Cheers, willzenstein

    Dear Willzenstein,

    Thank you so much for your prompt reply. I will look into it. May I ask you again if I have more question.

    Really appreciate your help ??

    Best,

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Length of excerpt’ is closed to new replies.