2012-03-10 7 views
1

私はPHPに新しいですし、the_excerpt()通話交換するhttp://aaronrussell.co.uk/legacy/improving-wordpress-the_excerpt/から、次のコードを使用しています:私はBrandford Magazine 3.0 themeを使用しています機能の1つのインスタンスを置き換えるフィルターではなく、他の

function improved_trim_excerpt($text) { 
    global $post; 
    if ('' == $text) { 
     $text = get_the_content(''); 
     $text = apply_filters('the_content', $text); 
     $text = str_replace('\]\]\>', ']]>', $text); 
     $text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text); 
     $text = strip_tags($text, '<p>'); 
     $excerpt_length = 80; 
     $words = explode(' ', $text, $excerpt_length + 1); 
     if (count($words)> $excerpt_length) { 
      array_pop($words); 
      array_push($words, '[...]'); 
      $text = implode(' ', $words); 
     } 
    } 
    return $text; 
} 

remove_filter('get_the_excerpt', 'wp_trim_excerpt'); 
add_filter('get_the_excerpt', 'improved_trim_excerpt'); 

を、そして2つのインスタンスを持っています私のコードでthe_excerpt()が呼び出されます。

何とか最初の呼び出しが正しく置き換えられ、improved_trim_excerptを使用してテキストを正しく表示していますが、もう1つの呼び出しで古いthe_excerpt()機能が使用されています。

私はここで何が欠けていますか?

答えて

0

どこでもthe_excerptは表示されませんが、preg_replaceを参照していると思いますか?その場合は、正規表現の末尾にgフラグを追加してみてください。これはグローバルモードをオンにするので、正規表現は複数回マッチします。

+0

'the_excerpt'は、この新機能が改善しようとしている機能です。リンク先のブログ記事のタイトルをご覧ください。 – Lazer

関連する問題