2013-07-28 45 views
0

根據標題長度是否可以控制摘錄的長度?根據標題長度控制摘錄

我的意思是,supposee你有25個字符的標題長度,它將把摘錄的20個字符,但如果你在職稱的有10個字符它把30個characaters ..

在此先感謝..

+0

有沒有你要找的字符的總數是多少? 25 + 20是45,而10 + 30是40. –

+0

不完全是,它無關緊要,它可以是任意組合.. – user1738483

回答

1

假設這是內環路:

function createExcerpt(){ 

    $title = get_the_title(); 
    $content = get_the_content(); 

    if (strlen($title) > 24){ 
     $custom_excerpt = substr($content, 0, 25); 
    } 

    else { 
     $custom_excerpt = substr($content, 0, 30); 
    } 

    return $custom_excerpt; 

} 

echo createExcerpt() . ' (...)'; 
+0

你認爲這是什麼意思? – user1738483

+0

在WordPress循環中。 get_the_title()和get_the_content()將不一定像循環外部那樣工作。 –

+0

這裏確實noob,只是測試它出現在文章代碼我猜那是什麼意思.. – user1738483

0
<?php 
    echo substr(the_title('', '', FALSE), 0, 40); 
?> 

<?php 
    if (strlen($post->post_title) > 40) { 
     echo substr(the_title($before = '', $after = '', FALSE), 0, 40) . '...'; 
    } else { 
     the_title(); 
    } 
?>