• Resolved Josh Maxwell

    (@tpschools)


    I’m creating a theme, but I have a problem with excerpts, I was wondering if I could get some help?

    The theme I’m creating has a Featured Post & a Recent Posts section on index.php. It has nothing to do with categories or most viewed posts or anything like that, it’s just different styling on the most recent/1st post while the 2nd & 3rd go under Recent Posts with their own styling.

    I’m using improved_trim_excerpt inside functions.php to elongate the_excerpt to 90 words for the Featured Post & is working fine. (Note: it works in such a way that users don’t have to use the Excerpt box on the New Post page. It will automatically grab X number of words from the post & add the […] on the end.)

    Now, in the Recent Posts Section (the 2nd & 3rd most recent posts) I wanted them to only show about 40 words. Here in lies the problem. I tried using a modified improved_trim_excerpt & set it as improved_trim_excerpt2 with no luck.

    I’m no expert on WP functions and could really use some help on this. If I need to provide more info, let me know.

Viewing 2 replies - 1 through 2 (of 2 total)
  • in functions.php:

    function excerpt_length_featured($length){
      return 90;
    }
    function excerpt_length_recent($length){
      return 40;
    }

    Featured Post:

    add_filter('excerpt_length', 'excerpt_length_featured');
    the_excerpt();
    remove_filter('excerpt_length', 'excerpt_length_featured');

    Recent Posts:

    add_filter('excerpt_length', 'excerpt_length_recent');
    the_excerpt();
    remove_filter('excerpt_length', 'excerpt_length_recent');

    Thread Starter Josh Maxwell

    (@tpschools)

    Thanks got it.

    // Excerpt function for Featured Post
    function featured_post_excerpt($text) {
    	global $post;
    	if ( '' == $text ) {
    		$text = get_the_content('');
    		$text = apply_filters('the_content', $text);
    		$text = str_replace(']]>', ']]>', $text);
    		$text = strip_tags($text, '');
    		$excerpt_length = 100;
    		$words = explode(' ', $text, $excerpt_length + 1);
    		if (count($words)> $excerpt_length) {
    			array_pop($words);
    			array_push($words, ' [...]');
    			$text = implode(' ', $words);
    		}
    	}
    	return $text;
    }
    
    // Excerpt function for Recent Posts
    function recent_post_excerpt($text) {
    	global $post;
    	if ( '' == $text ) {
    		$text = get_the_content('');
    		$text = apply_filters('the_content', $text);
    		$text = str_replace(']]>', ']]>', $text);
    		$text = strip_tags($text, '');
    		$excerpt_length = 30;
    		$words = explode(' ', $text, $excerpt_length + 1);
    		if (count($words)> $excerpt_length) {
    			array_pop($words);
    			array_push($words, ' [...]');
    			$text = implode(' ', $words);
    		}
    	}
    	return $text;
    }
    
    /* ----- Two Loops ----- */
    // Featured Post
    	remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    	add_filter('get_the_excerpt', 'featured_post_excerpt');
    	the_excerpt();
    	remove_filter('get_the_excerpt', 'featured_post_excerpt');
    	add_filter('get_the_excerpt', 'wp_trim_excerpt');
    
    // Recent Posts
    	remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    	add_filter('get_the_excerpt', 'recent_post_excerpt');
    	the_excerpt();
    	remove_filter('get_the_excerpt', 'recent_post_excerpt');
    	add_filter('get_the_excerpt', 'wp_trim_excerpt');
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Help with excerpt appreciated’ is closed to new replies.