logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$10
The best and simpliest way to run multiple excepts and strings

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.


Excerpt type one...

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


Excerpt type two...

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


Excerpt type three...

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


Excerpt type four...

<?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.

This question has been answered.

Josh Cranwell | 12/16/11 at 11:23am Edit


(3) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    12/16/11
    12:21pm
    Francisco Javier Carazo Gil says:

    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.

    • 12/16/11 1:18pm

      Josh Cranwell says:

      Hi Francisco,

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

      I do this...


      The Excerpt Lengths


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



      The Excerpt Lengths

      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');


      The Loop Code

      #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

    • 12/16/11 1:19pm

      Josh Cranwell says:

      Sorry the second The Excerpt Lengths should say The Excerpt Link Strings

    • 12/16/11 1:30pm

      Francisco Javier Carazo Gil says:

      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.

    • 12/19/11 6:27am

      Josh Cranwell says:

      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

    • 12/19/11 6:36am

      Francisco Javier Carazo Gil says:

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

      Regards.

  • avatar
    Last edited:
    12/16/11
    11:22pm
    Arnav Joy says:

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

    try this plugin

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

    • 12/19/11 6:20am

      Josh Cranwell says:

      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.

  • avatar
    Last edited:
    12/18/11
    3:32pm
    Julio Potier says:

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

    • 12/19/11 6:28am

      Josh Cranwell says:


      This works really well...

      <?php the_advanced_excerpt(); ?>

This question has expired.



Gabriel Reguly, Josh Cranwell, Julio Potier voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.