Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

The best and simpliest way to run multiple excepts and strings WordPress

  • SOLVED

Hi,

I've read many ways of creating multiple excepts lengths and different strings, but can someone tell me the best way to do it, as I can never get it right, its either one of the other. Usually because its too convoluted.

Lets say for example in my theme files, I got 4 different except types.


<strong>Excerpt type one...</strong>

<?php the_excerpt( length: '20', string: 'Read More...' ); ?>

<strong>Excerpt type two...</strong>

<?php the_excerpt( length: '45', string: 'Read Full Post &#8658;' ); ?>

<strong>Excerpt type three...</strong>

<?php the_excerpt( length: '80', string: 'Click to read' ); ?>

<strong>Excerpt type four...</strong>

<?php the_excerpt( length: '120', no string ); ?>


What would you say is the easiest way to execute the above example and control the strings and lengths in my functions.php or just inline via the_excerpt php like above.

The string will always be a link to the post using the post permalink.


I know it's sounds a bit simple, but for me doing lots of multiple excerpt types never seem to work as they should, defaulting back to the (...) string when using multiple lengths. Ahhh!


Thanks for your time.

Answers (3)

2011-12-16

Francisco Javier Carazo Gil answers:

Hi Josh,

You should use a filter:


function custom_excerpt_length( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );


And also this is interesting:

function new_excerpt_more($more) {
global $post;
return '<a href="'. get_permalink($post->ID) . '">Read the Rest...</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');


And remember to use the_excerpt() in your loop and fill it when creating.

You could also use <!--more--> inside your content. I prefer this one.


Josh Cranwell comments:

Hi Francisco,

See this confuses me, so lets say I need to recreate my example above...

I do this...


<strong>The Excerpt Lengths</strong>

function custom_excerpt_length( array
( $short ) { return 20; },
( $medium ) { return 45; },
( $long ) { return 80; },
( $bigger ) { return 120; }
)
add_filter( 'excerpt_length', 'custom_excerpt_length' );



<strong>The Excerpt Lengths</strong>

function custom_excerpt_more( array
( $readmore ){
return ' <a href="'. get_permalink($post->ID) . '">' . 'Read More...' . '</a>';},
( $fullpost ){
return ' <a href="'. get_permalink($post->ID) . '">' . 'Read Full Post &#8658;' . '</a>';},
( $clickread ){
return ' <a href="'. get_permalink($post->ID) . '">' . '&raquo; Continue Reading' . '</a>';}
)
add_filter('excerpt_more', 'custom_excerpt_more');


<strong>The Loop Code</strong>

#1 <?php the_excerpt( $short , $readmore ); ?>
#2 <?php the_excerpt( $medium , $fullpost ); ?>
#3 <?php the_excerpt( $long , $clickread ); ?>
#4 <?php the_excerpt( $bigger ); ?>



This obviously isn't going to work, I'm probably not even close, but how do you do multiple except lengths and string and control them via the_excerpt call? This is what is throwing me.

Thanks


Josh Cranwell comments:

Sorry the second <strong>The Excerpt Lengths</strong> should say <strong>The Excerpt Link Strings</strong>


Francisco Javier Carazo Gil comments:

Josh,

I think it's not possible to pass a parameter to the function my_excerpt_length(), but it is possible to use a global variable. so, you can add something like this to your functions.php file:

function my_excerpt_length() {
global $myExcerptLength;

switch ($myExcerptLength) {
case "short":
return 20;
break;
case "medium":
return 45;
break;
case "long":
return 80;
break;
case "bigger":
return 120;
break;
}
}
add_filter('excerpt_length', 'my_excerpt_length');


Remember to set correct value to global variable each time you need set it.


Josh Cranwell comments:

Hi Francisco,

Thanks for your idea but it did not solve the different link options.

The plugin below does everything I needed, and more just by adding settings into the <?php the_advanced_excerpt(); ?>

Pretty neat.

Thanks


Francisco Javier Carazo Gil comments:

Ok Josh, no problem. I also think that is always better using plugins when they are available and the work is done.

Regards.

2011-12-16

Arnav Joy answers:

you are using different excerpt for same page or different pages?

try this plugin

http://wordpress.org/extend/plugins/advanced-excerpt/


Josh Cranwell comments:

This is PERFECT and so simple.

You can control absolutely every thing with it...

<?php the_advanced_excerpt('length=70&use_words=0&no_custom=1&ellipsis=%26hellip;&exclude_tags=img,p,strong&read_more=view page&add_link=1'); ?>

Can't believe I missed this. Thanks.

2011-12-18

Julio Potier answers:

I'm trying to do this using a dynamic excerpt, i'll try again tomorrow.


Josh Cranwell comments:


This works really well...

<?php the_advanced_excerpt(); ?>