wordpress首页中每篇文章都是全文输出的, 不符合国人的习惯,这里讲述一个比较好的实现摘要的方法,优点:
1 适用于所有文章, 不用在文章中插入特殊的符号
2 保持文章的原有格式
以inove主题为例修改,其他主题修改相同的文件即可
在inove/functions.php 最后的
<?php } ?>
修改为
<?php } if (! function_exists('character_limiter')) { function character_limiter($str, $n = 300, $end_char = '……') { if (strlen($str) <= $n) { return $str; } $str = preg_replace("/\s+/", ' ', preg_replace("/(\r\n|\r|\n)/", " ", $str)); if (strlen($str) <= $n) { return $str; } $out = ""; foreach (explode(' ', trim($str)) as $val) { $out .= $val.' '; if (strlen($out) >= $n) { return trim($out).$end_char; } } } } ?>
inove/index.php 中
<?php the_content(__('Read more...', 'inove')); ?>
<?php print character_limiter($post->post_content,1200); ?> <p> </p> <p><a href="<?php the_permalink() ?>"><strong>继续阅读全文>></strong></a></p>
如果你使用了google-syntax-highlighter 插件, 会出现“继续阅读全文” 出现在代码段里面的问题, 如图
下面一片文章挺好的,但是上面的那一片文章就出现了错乱的问题。
在上面functions.php中修改的代码中
$out .= $val.' ';
if (strpos($val, '<pre') === 0){ return trim($out).$end_char; }
前提是你的代码都是圈在 <pre name="code" class="java"></pre>这样的标记中的