WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to?: Manual excerpt for RSS/Automatic excerpt for homepage/archives (3 posts)

  1. altfgcom
    Member
    Posted 2 years ago #

    In order to have our RSS feed feature different content than our posts, late last night we began manually inserting text into the "excerpt" field while adding new posts. It worked great, except...

    ... that now our homepage and tag / author archives (please see http://www.altfg.com) now display the full, lengthy manual description that we want to be found ONLY in the RSS feed.

    QUESTION: How can we keep the manual excerpt for the RSS feed BUT have the home page and the archive pages show the AUTOMATIC excerpt?

    Any assistance would be MUCH appreciated.

    We've tried different things that we saw at various WordPress forum topics last night, but all we managed to do was get several "parse errors" and 500 error messages...

    Currently, the site's functions.php file has the following code re: excerpt length (this applies ONLY to the automatic excerpt):

    [Note: the longer, 105, length was for our feed. "65" is the length for the excerpts on the site, e.g., homepage, archive pages.]

    if (function_exists('add_filter'))
    add_filter('excerpt_length', 'my_excerpt_length');
    function my_excerpt_length($length) {
    if( is_home() ) {
    $length = 65;
    } else {
    $length = 105;
    }
    return $length;
    }
    
    if (function_exists('add_filter'))
    add_filter('excerpt_length', 'new_excerpt_length');
    function new_excerpt_length($length) {
    if( is_archive() ) {
    $length = 65;
    }
    return $length;
    }
  2. popper
    Member
    Posted 2 years ago #

    Your solution is to add a filter to the_excerpt so that wordpress doesn't display the custom one:

    function make_excerpt() {
    	global $post;
    
    	$text = $post->post_content;
    
    	$text = strip_shortcodes( $text );
    
    	$text = apply_filters('the_content', $text);
    	$text = str_replace(']]>', ']]>', $text);
    	$excerpt_length = 55;
    	$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
    	$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
    
    	return $text;
    }
    
    add_filter('the_excerpt', 'make_excerpt');

    ps: the code above is basically the one found in wp-includes/formatting php that fakes the_excerpt if none exists with some filters removed. I kept the excerpt_more so that wp adds the "read more" link, but you can remove that or add your own. the_excerpt_rss used for your feed will display the custom one if it exists.

  3. altfgcom
    Member
    Posted 2 years ago #

    @popper,

    It worked *beautifully*.

    Can't tell you how thankful I am. This had been such a headache -- trying to find the right code, problems with duplicate content appearing on Google searches.

    You took care of it.

    Once again, *thank you*.

Topic Closed

This topic has been closed to new replies.

About this Topic