WordPress的自定义摘录长度不起作用

苏格兰人

我的functions.php中有此代码

function custom_excerpt_length() {

return 15;

}

add_filter('excerpt_length', 'custom_excerpt_length');

但它不起作用,因为它会给我全文而不是我指定的15个字。因此,我在我的网站上设置的网格不正确。

我的电影格

提前致谢

库尔迪普·马卡迪亚

还可以将此代码用于多种类型的摘录

function excerpt($limit) {
      $excerpt = explode(' ', get_the_excerpt(), $limit);
      if (count($excerpt)>=$limit) {
        array_pop($excerpt);
        $excerpt = implode(" ",$excerpt).'...';
      } else {
        $excerpt = implode(" ",$excerpt);
      } 
      $excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
      return $excerpt;
    }

    function content($limit) {
      $content = explode(' ', get_the_content(), $limit);
      if (count($content)>=$limit) {
        array_pop($content);
        $content = implode(" ",$content).'...';
      } else {
        $content = implode(" ",$content);
      } 
      $content = preg_replace('/\[.+\]/','', $content);
      $content = apply_filters('the_content', $content); 
      $content = str_replace(']]>', ']]>', $content);
      return $content;
    }

然后在您的模板代码中使用。

<?php echo excerpt(25); ?>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章