• Resolved ganesh.india

    (@ganeshindia)


    Hey i’m using <?php the_excerpt(); ?> function in Index.php and also filtering the number of character to show in functions.php

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

    Now actually i’m using the same the_excerpt() function somewhere else there also the length is 50.

    My problem is that i want 50 length at one place and 30 length at the another place. How i can do via the same function..

    Please help asap….

Viewing 8 replies - 1 through 8 (of 8 total)
  • Use IF statements with conditional tags, for example:

    add_filter('excerpt_length', 'my_excerpt_length');
    function my_excerpt_length($length) {
    	if ( is_home() )
    		return 50;
    	elseif ( is_category() )
    		return 30;
    	else
    		return $length;
    }

    Thread Starter ganesh.india

    (@ganeshindia)

    The problem is that both are on the same page….now what to do….the one which you have suggest will alter page to page….

    In my case both the excerpt are used on the same page….:(

    Can you be more specific? For what are you wanting to set excerpt length to 50 and for what to 30?

    Thread Starter ganesh.india

    (@ganeshindia)

    For the latest post which are displayed on home page for that 50 and at the bottom i’m querying post from category for which i want 30.

    Basically both are on the same page…..

    That’s easy. Just do it twice.

    Add before the first loop:

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

    Add before the second:

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

    Thread Starter ganesh.india

    (@ganeshindia)

    i did but its not working…..

    <?php global $post; $myposts = get_posts('numberposts=3&category=1'); foreach($myposts as $post) :setup_postdata($post);?>
    
    	<div class="sec-post">
    	    <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
    		<?php the_excerpt(); ?>
    	</div>
    <?php endforeach; ?>

    this is the code block for that….i tired the same earlier but didn’t get the desired output….nothing displayed in this section….

    Sorry, my mistake, can’t define the same function twice.

    Use different names and it should work. I’ve tested it.

    add_filter('excerpt_length', 'first_excerpt_length');
    function first_excerpt_length($length) { return 50; }
    add_filter('excerpt_length', 'second_excerpt_length');
    function second_excerpt_length($length) { return 30; }

    Thread Starter ganesh.india

    (@ganeshindia)

    Thank you very much it worked……

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to use different excerpt character’ is closed to new replies.