前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >怎样控制wordpress博客首页博文显示内容字数!

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

作者头像
爱游博客
发布2019-08-07 11:41:10
9940
发布2019-08-07 11:41:10
举报
文章被收录于专栏:爱游博客爱游博客

爱优博客是用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就是要限制的字节数

cambrian.render('tail')

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年9月24日1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
网站建设
网站建设(Website Design Service,WDS),是帮助您快速搭建企业网站的服务。通过自助模板建站工具及专业设计服务,无需了解代码技术,即可自由拖拽模块,可视化完成网站管理。全功能管理后台操作方便,一次更新,数据多端同步,省时省心。使用网站建设服务,您无需维持技术和设计师团队,即可快速实现网站上线,达到企业数字化转型的目的。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档