3

自由控制中英文文章输出字数

很多主题默认首页文章是全文输出,虽然在编辑时可以添加more标签,但还是麻烦而且不容易控制字数,通过下面的方法,修改一段代码即可完美实现自动化,适用于各种主题。

无插件WordPress中文截断方法

找到模板index.php中的

<?php the_content(); ?>

或者

<?php the_excerpt(); ?>

替换为

<?php echo mb_strimwidth(strip_tags(apply_filters
(’the_content’, $post->post_content)), 0, 300,”…”);?>

这个300就是你想要截断的数值,改为你认为合适的就可以了。

要实现自动截断英文,如下:

<?php $excerpt = get_the_excerpt();
echo string_limit_words($excerpt,25);?>

25是要截断的英文单词数。

同时实现截断中文与英文,如下:

<?php echo mb_strimwidth(strip_tags(apply_filters(’the_content’, $post->post_content)), 0, 100,”…”);?>
<?php } else { ?><?php $excerpt = get_the_excerpt();
echo string_limit_words($excerpt,25);?> <?php } ?>

100是中文数,25是英文数。

Leave a Reply