WordPress主题开发:截取标题或内容

截取可以用wp_trim_words()

用法:

<?php $trimmed = wp_trim_words( $text, $num_words = 55, $more = null ); ?>

参数说明:$text---截取内容,$num_words ---限定字数,$more ---截取后加在尾部的字符

 

截取文章内容:

<?php
    $content = get_the_content();
    $trimmed_content = wp_trim_words( $content, 40, '<a href="'. get_permalink() .'"> ...Read More</a>' );
    echo $trimmed_content;
?>

截取文章标题:

<?php
    $title = get_the_title();
    $trimmed_title = wp_trim_words( $title, 20, '...' );
    echo $trimmed_title;
?>

 

posted @ 2017-02-02 22:08  tinyphp  Views(926)  Comments(0Edit  收藏  举报