如何在 wordpress 中只显示文章摘要


有时候博客主页上只显示文章摘要是很有好处的, 比如文章过长的时候用户就不方便定位文章. 只在首页, 搜索页, Tag, 目录类别, 还有存档页显示文章标题, 还有一两句话摘要 则会让页面显得简洁许多.

其实你只需要编辑模板页面 content.php (推荐子主题),然后找到下面这行代码:

1
<?php if ( is_search() ) : // Only display Excerpts for Search ?>
<?php if ( is_search() ) : // Only display Excerpts for Search ?>

把它改成:

1
<?php if ( is_search() || is_home() || is_tag() || is_category() || is_archive() ) : // Only display Excerpts for Search ?>
<?php if ( is_search() || is_home() || is_tag() || is_category() || is_archive() ) : // Only display Excerpts for Search ?>

函数 is_archive() 检查当前页面是否是 目录 (is_category), 标签 (is_tag), 作者 或者是日期存档页面. 在下一行, 你可以看到用了 the_excerpt() 函数来显示文章摘要. 显示文章全文则需要用函数 the_content().

如果想在摘要后面加入全文链接, 则可以编辑模板文件 functions.php (最好在子主题里编辑), 在文件最后面加入以下内容.

1
2
3
4
5
6
7
8
9
10
11
// Remove the .. from excerpt and change the text
function change_excerpt_more()
{
  function new_excerpt_more($more)
    {
    // Use .read-more to style the link
      return '<span class="continue-reading"> <a href="' . get_permalink() . '">继续阅读 »</a></span>';
    }
  add_filter('excerpt_more', 'new_excerpt_more');
}
add_action('after_setup_theme', 'change_excerpt_more');
// Remove the .. from excerpt and change the text
function change_excerpt_more()
{
  function new_excerpt_more($more)
    {
    // Use .read-more to style the link
      return '<span class="continue-reading"> <a href="' . get_permalink() . '">继续阅读 »</a></span>';
    }
  add_filter('excerpt_more', 'new_excerpt_more');
}
add_action('after_setup_theme', 'change_excerpt_more');

这些修改并不影响 RSS Feed 里文章输出的格式. 如果想在FEED里显示文章摘要,则需要到设置-阅读, 然后更改相应的选项: “For each article in a feed, show” 改成 “总结 Summary” 而不是 “全文 Full Text”. 然而,这个设置会影响其它页面, 比如主页面和搜索页面.

英文同步: https://helloacm.com/showing-excerpts-in-wordpress-home-search-tag-and-archive-pages/

GD Star Rating
loading...
本文一共 298 个汉字, 你数一下对不对.
如何在 wordpress 中只显示文章摘要. (AMP 移动加速版本)
上一篇: 说说生孩子的那些事
下一篇: 投诉 npower 电气公司

扫描二维码,分享本文到微信朋友圈
8dd29acdfa10203174d20031a6208c78 如何在 wordpress 中只显示文章摘要 技术 折腾 程序设计 网站信息与统计

6 条评论

评论