您现在的位置是:首页 > 网站制作 > WordpressWordpress

使用 wp_trim_words() 截取限定字数的内容

杰帅2023-08-26【Wordpress】人已围观

简介WordPress 3.3 新增了一个 wp_trim_words() 函数,专门用来截取限定字数的内容,比如文章、摘要、标题等:

WordPress 3.3 新增了一个 wp_trim_words() 函数,专门用来截取限定字数的内容,比如文章、摘要、标题等:

<?php
echo wp_trim_words( get_the_content(), 100 ); // 文章内容
echo wp_trim_words( get_the_excerpt(), 100 ); // 文章摘要
echo wp_trim_words( get_the_title(), 100 ); // 文章标题
?>

当然,这个函数默认需要在循环中使用。

默认用法:

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

参数:

  • $text

    (字符串) (必需) 要截取的内容默认: 无
  • $num_words

    (整数) (可选) 限定的字数默认: 55
  • $more

    (字符串) (可选) 截取后加在尾部的字符默认: '&hellip;'

示例:

<?php
$content = get_the_content();
$trimmed_content = wp_trim_words( $content, 40, '<a href="'.%20get_permalink()%20.'"> ...阅读更多</a>' );
echo $trimmed_content;
?>

Tags:wordpress   截取

很赞哦! ()

文章评论

本站推荐

站点信息

  • 建站时间:2018-10-24
  • 网站程序:帝国CMS7.5
  • 主题模板《今夕何夕》
  • 文章统计1172篇文章
  • 标签管理标签云
  • 统计数据百度统计
  • 微信公众号:扫描二维码,关注我们