dongwei1855 2014-12-19 21:43
浏览 42
已采纳

php max excerpt length不能长度(不包括html / css)

I have a problem using the below:

<?php echo excerpt(15); ?>

Because! I have some pages (unfortunately) that have on-page styles and little text, not meeting the 15 characters, so what is occurring is the excerpt limit is not being met, so it displays 'a little text.. then <css> <styles><h1> <h2>, etc'.


How can I re-write as UP to 15 character limit, or MAX. So if it falls short that's fine. Or to exclude HTML / and CSS?


Excerpt function within functions.php

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(']]>', ']]&gt;', $content);
      return $content;
    }

remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'custom_trim_excerpt');

function custom_trim_excerpt($text) { // Fakes an excerpt if needed
global $post;
if ( '' == $text ) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
$text = strip_tags($text);
$excerpt_length = 35;
$words = explode(' ', $text, $excerpt_length + 1);
if (count($words) > $excerpt_length) {
array_pop($words);
array_push($words, '...');
$text = implode(' ', $words);
}
}
return $text;
}
  • 写回答

1条回答 默认 最新

  • ds08541 2014-12-19 21:52
    关注

    You have three options (possibly more, this isn't meant to be exhaustive)

    1. Abandon the wordpress function. By using custom PHP passing the $post->post_content through to your own function you could use PHP's XML manipulation functions, specifically DOM manipulation, or a regular expression to remove certain tags.

    2. Do a check on your post content before running excerpt. If you can check for the existence of a tag to start within the first 15 characters by checking for the first position of a "<" character using strpos, then you can use a normal if/else.

    3. Using wordpresses functionality properly, you can change how the excerpt is dealt with through filters. You can remove the default functionality and replace it with your own. This link in particular is very useful for explaining how to customise your excerpt function in wordpress, specifically with regards to allowing tags or not within the excerpt.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题