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

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

爱优博客是用wordpress程序做的,但是默认的wordpress首页博客文章的内容显示很,整个页面显示不了几篇文章,有时写的文章全部都显示在了首页了。自己在网站搜索了下找到了5种不用插件解决文章显示字数的方法:1. 使用the_excerpt标签 (缺点:需要改动模版,...

爱优博客是用wordpress程序做的,但是默认的wordpress首页博客文章的内容显示很,整个页面显示不了几篇文章,有时写的文章全部都显示在了首页了。

自己在网站搜索了下找到了5种不用插件解决文章显示字数的方法:

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

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

找到

  1. <?php the_content(__(’(more…)’)); ?>

  1. <?php the_content(); ?>

修改为:

  1. <?php if(!is_single()) {
  2. the_excerpt();
  3. else {
  4. the_content(__(‘(more…)’));//或者<?php the_content(); ?>
  5. } ?>

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

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

中的

  1. <?php the_content(); ?>

修改为:

  1. <?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、修改

  1. <?php the_content(); ?>

为下面的

  1. <?php if (is_single()) {
  2. the_content(‘Read the rest of this entry &raquo;’);
  3. else {
  4. the_excerpt();
  5. }
  6. ?>

或者

  1. <?php if(is_category() || is_archive() || is_home() ) {
  2. the_excerpt();
  3. else {
  4. the_content(‘Read the rest of this entry &raquo;’);
  5. } ?>

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

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

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

  1. <?php the_title(); ?>

替换成

  1. <?php echo excerpttitle(23);?>

其中的23就是要限制的字节数

原文链接:https://www.vipiu.net/archives/2017/09/24/25.html,转载请注明出处。
0

评论0

请先

会员低至49元,开通享海量VIP资源免费下载 自助开通
显示验证码
没有账号?注册  忘记密码?