wordpress中在作者页面经常需要显示作者的简介,但是有些作者简介的文字比较多,而我们只需要截取其中一段字符串即可,以下为截取作者简介的方法:
在你wordpress主题的functions.php文件中添加如下代码:
- <?php
- function author_excerpt (){
- $word_limit = 20; // 限制字数
- $more_txt = '阅读更多:'; // 阅读更多文本
- $txt_end = '...'; // 添加在文本末尾
- $authorName = get_the_author();
- $authorUrl = get_author_posts_url( get_the_author_meta('ID'));
- $authorDescriptionShort = wp_trim_words(strip_tags(get_the_author_meta('description')), $word_limit, $txt_end.'<br /> '.$more_txt.' <a href="'.$authorUrl.'">'.$authorName.'</a>');
- return $authorDescriptionShort;
- }
- ?>
在你需要显示作者简介的地方插入如下代码:
- <?php if (function_exists('author_excerpt')){echo author_excerpt();} ?>