怎样控制wordpress博客首页博文显示内容字数

用wordpress程序做博客的各位都知道,默认的wordpress首页博客文章的内容显示很长,整个页面显示不了几篇文章,而且占据了很长的网页,有时写的文章全部都显示在了首页了。看起来很不美观!如何进行处理,让每篇文章的显示字节数在控制的范围呢?


从网上搜索了下,找到了5种不用插件解决文章显示字数的方法:


1. 使用the_excerpt标签 (缺点:需要改动模版,而且显示的是纯文本。优点:一劳永逸直接把想要的部分来做摘要)

使用方法,注意是编辑你的模版中(wp-contant/themes/你的模版/index.php) 文件)。

找到

<?php the_content(__('(more…)')); ?>

<?php the_content(); ?>

修改为:

<?php if(!is_single()) {
     the_excerpt();
     } else {
     the_content(__('(more…)'));//或者<?php the_content(); ?>
  } ?>


2.使用 php 自带的mb_strimwidth函数 功能强大

将 wp-contant/themes/你的模版/index.php 文件

中的 <?php the_content(); ?>

修改为:

<?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 输入数字,"……"); ?>

输入数字:你想要显示的文章字数


3.使用more标签 (缺点:每次都要加一下这个东西,不灵活只能一刀切。优点:方法比较正规不需要改动模版)

在你需要截断的地方(就是你的编辑框)加

<!-more->

代码.
在<!–more–>在之前的内容非单篇post的情况下作为摘要显示。
小提示:如何在quicktags栏中显示more按钮。
在你使用的theme的index.php中查找<?php the_content(); ?>,如找到,用<?php the_content(__(’(more…)’)); ?>覆盖。


4、修改<?php the_content(); ?>为下面的
<?php if (is_single()) {
  the_content(‘Read the rest of this entry &raquo;’);
} else {
  the_excerpt();
}
?>
或者
<?php if(is_category() || is_archive() || is_home() ) {
the_excerpt();
} else {
the_content(‘Read the rest of this entry &raquo;’);
} ?>


5.在functions.php最后加入以下代码:

<?php
function excerpttitle($max_length) {
$title_str = get_the_title();
if (mb_strlen($title_str,’utf-8′) > $max_length ) {
$title_str = mb_substr($title_str,0,$max_length,’utf-8′).’…’;
}
return $title_str;
}?>

这就定义了一个函数,如何调用呢在你想要使用字数限制的页面,将原有的

<?php the_title(); ?>
替换成

<?php echo excerpttitle(23);?>
其中的23就是要限制的字节数


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值