2013-07-28 44 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(); 
    } 
?>