2010-12-07 13 views
0

Wordpressの抜粋長を変更するためにスタックオーバーフローで見つかったクラスを変更しました。それはクマだった(私はOOPを初めて使ったので)が、最終的には動作し、より多くのリンクをフィルタリングするための2番目のパラメータを受け入れる。私は何をしたいと思いますか、現在出力は '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()でthe_excerpt()の代わりにget_the_excerpt()を使用し、Excerpt :: filter関数で対応する戻り値を追加し、 my_excerpt関数を呼び出し、最後にこの関数を追加します。

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

はい!あなたが言ったことに加え、静的出力()の戻り値を追加し、今それが動作します。元の投稿のコードを修正して編集内容を反映させました。ありがとうございました! – helgatheviking

関連する問題