如何在WordPress主题中显示文章摘录

Some of our users wanted us to post a tutorial on how to display Post Excerpts in WordPress Themes. This is a very simple tutorial that anyone can implement and take advantage of this built-in feature. The benefit of implementing this tutorial is that it decreases page load time and it increases the pageview count.

我们的一些用户希望我们发布有关如何在WordPress主题中显示帖子摘录的教程。 这是一个非常简单的教程,任何人都可以实施并利用此内置功能。 实施本教程的好处在于,它减少了页面加载时间,并且增加了浏览量。

How does it Decrease Page Load Time?

如何减少页面加载时间?

If you write long posts like best practices of contact forms or must have iphone apps for bloggers which has a lot of images, then having them load on one page can significantly impact your page load time. By adding this technique, you can simply show a mini description of the article and let user view the post on single post page.

如果您写长篇文章(如联系表单的最佳做法),或者必须为博客创建iPhone应用程序且包含很多图像,则将它们加载到一页上会显着影响页面加载时间。 通过添加此技术,您可以简单地显示文章的迷你说明,并让用户在单个帖子页面上查看帖子。

How does it Increase Page View count?

如何增加页面浏览量?

Sometimes users just read the post on the category page if you don’t use the excerpt. By adding it this way, they will go to the original post. That is 2 page views instead of 1. It also encourages users to make a comment on the post because comment form is also on this page and they don’t need to load a separate page to make a comment.

有时,如果您不使用摘录,则用户只会在类别页面上阅读帖子。 通过这种方式添加,他们将转到原始帖子。 那是2个页面视图,而不是1个页面视图。它也鼓励用户在帖子上发表评论,因为评论表单也在此页面上,并且他们无需加载单独的页面来发表评论。

讲解 (Tutorial)

Open your index.php file, archive.php file, and category.php file. Now some of you might not have all these files in your template, so just open the ones that you do have.

打开index.php文件, archive.php文件和category.php文件。 现在,您中的某些人可能未在模板中拥有所有这些文件,因此只需打开您确实拥有的文件即可。

Find the following code:

查找以下代码:

<?php the_content(); ?>

And replace it with:

并替换为:

<?php the_excerpt(); ?>

Now you can write custom excerpts for your posts from your WordPress admin panel and have it displayed in your theme.

现在,您可以从WordPress管理面板中为帖子编写自定义摘录,并将其显示在主题中。

Add Post Excerpts in WordPress

If you do not write a custom excerpt, then WordPress automatically takes the first 55 words from your post and put them in an excerpt with elipses and display it. You can change the word limit starting from WordPress 2.9. If you want to change the word limit open your functions.php file and add the following function:

如果您不编写自定义摘录,则WordPress会自动从您的帖子中提取前55个单词,并将其放在带有省略号的摘录中并显示出来。 您可以从WordPress 2.9开始更改字数限制。 如果要更改字数限制,请打开functions.php文件并添加以下功能:


// Changing excerpt length
function new_excerpt_length($length) {
return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');

// Changing excerpt more
function new_excerpt_more($more) {
return '...';
}
add_filter('excerpt_more', 'new_excerpt_more');

If you want to see this code working live, just click on any of our categories from the navigation menu and you will see that we do not display full posts.

如果您想观看此代码的实时运行,只需在导航菜单中单击我们的任何类别,您就会看到我们没有显示完整的帖子。

翻译自: https://www.wpbeginner.com/wp-themes/how-to-display-post-excerpts-in-wordpress-themes/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值