2016-11-12 8 views
0

いくつかの研究とカスタマイズされた抜粋機能を書いた後、私は自分の問題を部分的に解決することができました。私がしたいと思っていたのは、chracterベースの抜粋機能を書いている間に、同じ箇所にあるすべての抜粋を終了することでした...しかし、それでもそれはできません。機能を見てみてください。おそらくあなたは何が改善されるか考えています。Wordpressのカスタム抜粋文字ベース - 微調整

My aim was to create excerpt always same lenght despite the content

function get_excerpt(){ 
$excerpt = get_the_content(); 
$excerpt = strip_shortcodes($excerpt); 
$excerpt = strip_tags($excerpt); 
$excerpt = substr($excerpt, 0, 480); 
$excerpt = substr($excerpt, 0, strrpos($excerpt, ' ')); // End with full word 
$excerpt = $excerpt.'...'; 
return $excerpt; 
} 

答えて

0

CześćMKB!しかし、それを試してみてくださいP:まだ

function get_excerpt(){ 
    $excerpt = get_the_content(); 
    $excerpt = strip_shortcodes($excerpt); 
    $endNearPos = 480; 

    $temp1 = strrpos (substr($excerpt, 0, $endNearPos) , ' '); 
    $temp2 = strpos ($excerpt , ' ', $endNearPos); 

    if(abs($endNearPos - $temp1) > abs($endNearPos - $temp2)){ 
     return substr($excerpt, 0, $temp2).'...'; 
    } else { 
     return substr($excerpt, 0, $temp1).'...'; 
    } 
} 

なく、同じ場所ではなく近いあなたは「同じ場所で終わる」または「完全な単語で終わる」の間を選択する必要があります。

+0

ジンジキー!今のところ十分に近いですが、実際のデータではうまく機能しているかどうかをお知らせします。 –

関連する問題