Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End WP: Different excerpt length for different pages

  • This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #30243
    leonpaul
    Member

    Hi all,
    I’m using this code in my functions.php file in WP to customize my excerpt length

    	function lyo_excerpt_length( $length ) {
    return 34;
    }
    add_filter( 'excerpt_length', 'lyo_excerpt_length' );

    This works fine, but I’d like to have my “Articles” page display excerpt with a shorter length than my “index” page. Is this possible? I’m not really good with conditional tags…
    Thanks in advance
    Léon-Paul Schwab

    #79775
    Bob
    Member

    Pages have page IDs, which are added to the body tag, like

    . I think this can be done with a PHP if statement, but I don’t really know how. You can find the page ID in your source code or the wordpress admin panel.
    I’ll see if I can help you later on.

    #79514
    leonpaul
    Member

    Hey,
    Thanks for your reply.
    I’m pretty sure I can find the page ID, I’m just not sure how to integrate the condition in the above-mentionned function…

    #79548
    Bob
    Member

    Hmm, I tried to help you but it didn’t work yet. Maybe some one else can shed some light on this case?
    So far, I came up with this:
    This is what I added to my functions.php file:


    if ($page_id == "21") {

    function new_excerpt_length($length) {
    return 30;
    }
    add_filter('excerpt_length', 'new_excerpt_length');

    }
    elseif ($page_id == "23") {

    function new_excerpt_length($length) {
    return 10;
    }
    add_filter('excerpt_length', 'new_excerpt_length');

    }

    else {

    function new_excerpt_length($length) {
    return 50;
    }
    add_filter('excerpt_length', 'new_excerpt_length');

    }

    Basically, what I did was I tried to get the page id by using the $page_id variable, and then match it to some value using an if elseif statement. I tested it on my local copy of WP (after having made pages with id’s of 21 and 23), but for some reason, they still have a excerpt length of 50 characters. Perhaps my if statement is wrong, or the functions.php doesn’t know the $page_id variable? I know it exists, as pointed out here.

    #79569
    leonpaul
    Member

    Hi,
    yep, I feel we’re on the right track. As in my case, it’s mostly to differentiate my home page from the “articles” page, I tried to use is_home(). What that did is that it changed the excerpt length of my first post, on both my home page and articles page, which not really the desired result.
    Thanks a lot for your help though, I’ll try to tweak around with what you gave.

Viewing 5 posts - 1 through 5 (of 5 total)
  • The forum ‘Back End’ is closed to new topics and replies.