WordPress.org

Forums

custom length excerpt in one template only (9 posts)

  1. Robert Simpson
    Member
    Posted 4 years ago #

    Hi folks,

    I'm working on a new site where part of one (long) page is being displayed on the homepage and I'd like to use the excerpt to cut it off after so many words before having it display a "continue reading..." link.

    Now, I know how to change the length of the excerpt on a site wide basis and that's all good, but in this one instance I'd like to make it longer, is this possible?

    Cheers,
    Rab

  2. Jeremy Pry
    Member
    Posted 4 years ago #

    You may be able to do something like this (but I haven't tested it to see if it will actually work):

    function custom_excerpt_length( $length ) {
       if ( is_page( /*Page ID goes here*/ ) ) {
          return 20;
       }
    }
    add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
  3. Robert Simpson
    Member
    Posted 4 years ago #

    Hi JPry,

    Would the page ID I'm testing for be the page the post appears on or the page I want to change the excerpt length on?

    Cheers,
    Rab

  4. nickaster
    Member
    Posted 4 years ago #

    Hi guys - I'm trying to do the same thing... isn't there a way to do this and avoid messing with functions.php alltogether?

    ie - can't you just say "display first XX words of the excerpt here" and be done with it?

  5. Robert Simpson
    Member
    Posted 4 years ago #

    *bump*

  6. Michael
    Forum Moderator
    Posted 4 years ago #

    I'm working on a new site where part of one (long) page is being displayed on the homepage and I'd like to use the excerpt to cut it off after so many words before having it display a "continue reading..." link.

    page as in 'static page' ?

    excerpts don't really work with pages.

    you could try <!--nextpage--> instead.
    http://codex.wordpress.org/Styling_Page-Links

    or add the filter function and call into the template before the loop(instead of into functions.php)
    i have not widely tried it, but it seems possible.

    <?php	function excerpt_length_for_template($length) {
    	return 5;
    }
    add_filter('excerpt_length', 'excerpt_length_for_template');
    ?>
  7. Robert Simpson
    Member
    Posted 4 years ago #

    If excerpts don't work well with static pages, could I load it in with the_content(), trim the number of words and add a continue link to the end?

  8. Michael
    Forum Moderator
    Posted 4 years ago #

    not sure, because the continue link would point to the same page which would only show the short form - kind of catch-22

    (i tried to overcome this by checking the referring link url:
    http://codex.wordpress.org/Function_Reference/wp_get_referer
    and just using the last part after the / )

    a possible hack is here:
    in page.php (or your page template) replace <?php the_content(); ?> with:
    http://pastebin.com/y0C0569V

    (not widely tested at all;
    does not work together with <!--nextpage-->

    edit:
    fix for that: http://pastebin.com/WSyXCJAy )

  9. Michael
    Forum Moderator
    Posted 4 years ago #

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags