Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3496750
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:17:14+00:00 2026-05-18T12:17:14+00:00

I’ve been modifying a class I found on Stack Overflow for varying WordPress’ excerpt

  • 0

I’ve been modifying a class I found on Stack Overflow for varying WordPress’ excerpt length. It has been a bear (since I am new to OOP) but it finally does work and accept a 2nd parameter now to filter the read more link. What i would like to do though, is currently the output is ‘the_excerpt’ which echos out immediately whereever the function “my_excerpt()” is called. I’d like to add a function called “get_my_excerpt” that returns the value. I know get_the_excerpt() does exactly that, but I can’t seem to make it work in this class.

  /* Class that enables excerpt length parameter */
/* called via my_excerpt('length') */

class Excerpt {

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

   // Default more (by WordPress)
  public static $more = "[...]";

  // So you can call: my_excerpt('short');
  public static $types = array(
      'short' => 25,
      'regular' => 55,
      'long' => 100,
      'xlong' => 200,
    );

    // So you can call: my_excerpt('short');
    public static $more_types = array(
      'none' => "",
      'regular' => "[...]",
      'ellipse' => "...",
      'permalink' => 'skip',
    );




  /**
   * Sets the length for the excerpt,
   * then it adds the WP filter
   * And automatically calls the_excerpt();
   *
   * @param string $new_length 
   * @return void
   * @author Baylor Rae'
   */
  public static function filter($new_length = 55, $new_more="[...]", $echo=TRUE) {
    Excerpt::$length = $new_length;
    Excerpt::$more = $new_more;

    add_filter('excerpt_length', 'Excerpt::new_length',98);
    add_filter('excerpt_more', 'Excerpt::new_more',99);

    return 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;
  }

   // Tells WP the new more
  public static function new_more() {

  $db = new ReadMore;

    if( isset(Excerpt::$more_types[Excerpt::$more]) AND ( (Excerpt::$more_types[Excerpt::$more]) != "skip" ) )
      return Excerpt::$more_types[Excerpt::$more];
    elseif( isset(Excerpt::$more_types[Excerpt::$more]) AND ( (Excerpt::$more_types[Excerpt::$more]) == "skip" ) )
      return $db->readmore();
    else
      return Excerpt::$more;
  } 

  // Echoes out the excerpt
  public static function output() {
    return get_the_excerpt();
  }



}

// An alias to the class
function get_my_excerpt($length = 55, $more="[...]") {
  return Excerpt::filter($length, $more);
}

// An alias to the class
function my_excerpt($length = 55, $more="[...]") {
  echo get_my_excerpt($length, $more);
}


class ReadMore {
  private $title;
  private $permalink;
  private $more;


  public function __construct () {
    //$this->title = get_the_title();
    //$this->permalink = get_permalink();
    $temp = "..." . '<a class="readmore" title="'. _('Permalink to').get_the_title() . '" href=" ' . get_permalink() . '">'._('Read the rest').'</a>';
    $this->more = $temp;

  }
  public function readmore() {    
    return $this->more;
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T12:17:15+00:00Added an answer on May 18, 2026 at 12:17 pm

    if you don’t want to rewrite or copy-paste some code I think this code could help, the code isn’t elegant but works, just add this function:

    function get_my_excerpt($length = 55, $more="[...]") {
      ob_start();
      Excerpt::filter($length, $more);
      $my_excerpt = ob_get_contents();
      ob_end_clean();
    }
    

    I guess that a better way to solve this is to rewrite some code, for example, use get_the_excerpt() instead of the_excerpt() in the static function output(), add the corresponding return in the Excerpt::filter function, add an echo in the my_excerpt function and finally add this function:

    function get_my_excerpt($length = 55, $more="[...]") {
      return Excerpt::filter($length, $more);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I'm looking for suggestions for debugging... If you view this site in Firefox or
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.