wordpress 技巧:显示作者简介概要


wordpress中在作者页面经常需要显示作者的简介,但是有些作者简介的文字比较多,而我们只需要截取其中一段字符串即可,以下为截取作者简介的方法:
在你wordpress主题的functions.php文件中添加如下代码:

  1. <?php
  2. function author_excerpt (){
  3. $word_limit = 20; // 限制字数
  4. $more_txt = '阅读更多:'; // 阅读更多文本
  5. $txt_end = '...'; // 添加在文本末尾
  6. $authorName = get_the_author();
  7. $authorUrl = get_author_posts_url( get_the_author_meta('ID'));
  8. $authorDescriptionShort = wp_trim_words(strip_tags(get_the_author_meta('description')), $word_limit, $txt_end.'<br /> '.$more_txt.' <a href="'.$authorUrl.'">'.$authorName.'</a>');
  9. return $authorDescriptionShort;
  10. }
  11. ?>

在你需要显示作者简介的地方插入如下代码:

  1. <?php if (function_exists('author_excerpt')){echo author_excerpt();} ?>