WordPress输出自定义长度摘要

首页 论坛 WordPress讨论区 综合技术 WordPress输出自定义长度摘要

标签: ,

该主题包含 0 个回复,有 1 个参与人,并且由  管理员11 年, 10 月 前 最后一次更新。

正在查看帖子 1(共 1 个)
  • 作者
    帖子
  • #922

    管理员
    管理员

    @admin

    通常我们有时需要在道面输出文章的摘要,但因为要兼顾界面的美观,所以在不同页面输出不同长度的摘要,这个时候可以自己写一个摘要输出函数即可,函数代码如下,将如下代码复制并拷贝到主题的(functions.php)文件即可。

     Code: arbitrary (select
    1.
    2.
    3.
    4.
    5.
    6.
    7.
    8.
    9.
    10.
    11.
    12.
    13.
    14.
    15.
    16.
    17.
    18.

    /*-----------------------------------------------------------------------------------*/
    /*  excerpt   摘要输出更变字数函数
    /*-----------------------------------------------------------------------------------*/
    function excerpt($limit) {
      $raw_excerpt = $text;
      if ( '' == $text ) {
        $text = get_the_content('');

        $text = strip_shortcodes( $text );

        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
        $excerpt_length = apply_filters('excerpt_length', $limit);
        $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
        $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
      }
      return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
    }

    使用方法,可以在index.php等面页文件中用 如:excerpt(40) 替换get_the_content(”),或the_excerpt()等文章调用函数,即输出40字摘要。

    • 该主题由  管理员 于 11 年, 10 月 前 修正。
正在查看帖子 1(共 1 个)

抱歉,回复评论必需登录。