Individual Excerpt lengths in BranfordMagazine

Individual Excerpt lengths in BranfordMagazine

14.01.2010 in Tutorials

BranfordMagazine and also my other themes use the excerpt template tag for auto-shortening texts on the homepage and in other areas. This is nothing spectacular since many themes do it like that. But BranfordMagazine uses the power of WordPress 2.9x to provide a function for individual excerpt lengths. Be aware that this doesn´t work with WP versions prior to 2.9. By default this is used for shorter excerpts in the featured articles section. Of course you can use this technique with any of my premium WordPress themes.

How to use individual excerpt lengths

Open your /tools/various-functions.php file in the theme directory and look up the code below. You will see this (or a similar) function:

<?php
//modify the excerpt lenght in the "featured articles" section on the homepage
add_filter(‘excerpt_length’, ‘my_excerpt_length’);
function my_excerpt_length($length) {
    if(in_category(4)) { // put here the ID (4 by default) of the category used for the "featured" articles
        return 25; // put the number of words (default 25)
    } else {
        return 55; // show 55 words for any other excerpt (or change this number also)
    }
}
?>

I think it´s more or less self explaining. By default (and based on my demo site) it means that the excerpt of category 4 (featured articles) is limited to 25 words. The excerpt of any other category is limited to 55 words which is the WordPress default. Now you can easily change the lenght of your excerpts just by changing the numbers 25 and/or 55 to whatever you like.

Extending the function

You can also extend this function to archieve other categories to display different excerpt lengths. Try something like this:

<?php
//modify the excerpt lenght in the "featured articles" section on the homepage
add_filter(‘excerpt_length’, ‘my_excerpt_length’);
function my_excerpt_length($length) {
    if(in_category(4)) { // put here the ID (4 by default) of the category used for the "featured" articles
        return 25; // put the number of words (default 25)
    }
    elseif(in_category(1)) { // put here the ID of the category
        return 75; // put the number of words (default 25)
    }
    else {
        return 55; // show 55 words for any other excerpt (or change this number also)
    }
}
?>

It´s up to you now. Play around with it.


,