0

I have written a some code to get custom excerpt_length and excerpt_more. The excerpt_length is working for both custom post type and general post. But I don't know why excerpt_more is not working for custom post types. Below is my code.

class Excerpt {

  // Default length (by WordPress)
  public static $length = 55;

  // So you can call: doctoplus_excerpt('short');
  public static $types = array(
      'ten'=>10,
      'few' => 15,
      'short' => 25,
      'regular' => 55,
      'long' => 100
    );
  //Default more
  public static $more = '...';

  //So you can call doctoplus_excerpt('short','Read More');
  public static $moreTypes = array(
    1 => 'Read More',
    2 => 'More',
    3 => 'View more'
  );
  /**
   * Sets the length for the excerpt,
   * then it adds the WP filter
   * And automatically calls the_excerpt();
   *
   * @param string $new_length 
   * @return void
   * @author Girija
   */
  public static function length($new_length = 55) {
    Excerpt::$length = $new_length;

    add_filter('excerpt_length', 'Excerpt::new_length', 999);

    Excerpt::output();
  }

  // Tells WP the new length
  public static function new_length() {
    if( isset(Excerpt::$types[Excerpt::$length]) )
      return Excerpt::$types[Excerpt::$length];
    else
      return Excerpt::$length;
  }

  public static function more($new_more = 'Read More') {
    Excerpt::$more = $new_more;
    add_filter('excerpt_more', 'Excerpt::new_more', 999);
  }

  //Tell WP the custom more
  public static function new_more() {
    global $post;
    if(isset(Excerpt::$moreTypes[Excerpt::$more]) ) {
      return ' <a class="more-link" href="'. get_permalink($post->ID) . '">'. __(Excerpt::$moreTypes[Excerpt::$more], 'doctoplus') .'</a>';
    } else {
      return Excerpt::$more;
    }
  }
  // Echoes out the excerpt
  public static function output() {
    the_excerpt();
  }

}

// An alias to the class
function doctoplus_excerpt($length = 55, $more = "...") {
  Excerpt::length($length);
  Excerpt::more($more);
}

Can any one help me? I really need this to be done. Thanks in advance.

| improve this question | | | | |
  • Please don't use the wordpress.org tag. It is reserved for codex related questions – Pieter Goosen May 22 '14 at 11:09
  • You have written an extensive class to display excerpts. There are a shorter quicker less extensive way to accomplish multiple excerpt lenghts. I have done a complete answer on the_excerpt() here. Hope that will help – Pieter Goosen May 22 '14 at 11:17
  • are you sure your post types support 'excerpt'? check your register_post_type arguments. – MortalViews May 22 '14 at 11:30
  • @PieterGoosen I know that. But I used this complex way so that i can use different read more and different excerpt_length by just passing the values to my function doctoplus_excerpt. – John Hack May 22 '14 at 12:25
  • @MortalViews Is there any extra attribute used for enabling excerpt when creating custom post type. – John Hack May 22 '14 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.