2010-12-07 26 views
0

我一直在修改一個類,我發現堆棧溢出不同的Wordpress'摘錄長度。它一直是熊(因爲我是OOP的新手),但它現在終於可以工作並接受第二個參數來過濾閱讀更多鏈接。我想要做的是,當前輸出是'the_excerpt',無論函數「my_excerpt()」被調用,它立即回聲。我想添加一個名爲「get_my_excerpt」的函數來返回值。我知道get_the_excerpt()完全是這樣,但我似乎無法讓它在這個類中工作。返回從一個類的多個wordpress摘要get_the_excerpt()

/* 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

,如果你不希望重寫或複製粘貼一些代碼,我覺得這個代碼可以幫助,代碼不優雅,但作品,只是添加此功能:

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

我猜測解決這個問題的更好方法是重寫一些代碼,例如,在靜態函數output()中使用get_the_excerpt()而不是the_excerpt(),在Excerpt :: filter函數中添加相應的返回值, my_excerpt函數,最後加入這個函數:

function get_my_excerpt($length = 55, $more="[...]") { 
    return Excerpt::filter($length, $more); 
} 
+0

是的!做了你所說的話,再加上靜態輸出()的返回值,現在它可以工作。我已更正原始帖子中的代碼,以反映您的修改。謝謝! – helgatheviking 2010-12-07 15:39:25