Note that there are some explanatory texts on larger screens.

plurals
  1. POreturn get_the_excerpt() from a class for multiple wordpress excerpts
    primarykey
    data
    text
    <p>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.</p> <pre><code> /* 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' =&gt; 25, 'regular' =&gt; 55, 'long' =&gt; 100, 'xlong' =&gt; 200, ); // So you can call: my_excerpt('short'); public static $more_types = array( 'none' =&gt; "", 'regular' =&gt; "[...]", 'ellipse' =&gt; "...", 'permalink' =&gt; '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-&gt;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-&gt;title = get_the_title(); //$this-&gt;permalink = get_permalink(); $temp = "..." . '&lt;a class="readmore" title="'. _('Permalink to').get_the_title() . '" href=" ' . get_permalink() . '"&gt;'._('Read the rest').'&lt;/a&gt;'; $this-&gt;more = $temp; } public function readmore() { return $this-&gt;more; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload