tom*_*101 1 html github liquid jekyll github-pages
这是我目前用来查看博客文章是否超过 120 个字的代码片段。如果为 true,请截断内容并在帖子底部添加“阅读更多”链接。
<div class="blogs">
{% for post in site.posts %}
<article class="post">
<h3><a href="{{ site.baseurl }}/{{ site.blogs }}{{ post.title }}">{{ post.title }}</a></h3>
{% assign wordCount = {{ post.content | size }} %}
{% if wordCount > 120 %}
<div class="entry">
{{ post.content | truncatewords:120}}
</div>
<a href="{{ site.baseurl }}{{ post.url}}" class="read-more">Read More</a>
{% else %}
{{ post.content }}
{% endif %}
</article>
{% endfor %}
</div>
Run Code Online (Sandbox Code Playgroud)
当我提交这个时,我收到一封来自 GitHub 的电子邮件,说构建时出现页面错误,并且没有打印出任何其他内容。
我猜这与{{ post.content }}无法过滤掉尺寸有关,所以我被卡住了。
我如何获得单个博客文章的字数,以便我能够截断一些超过 120 个字的文章?提前致谢。
{{ post.content | size }} 为您提供 post.content 字符串中的字符数。
如果要计算字符串中的单词,请使用number_of_words液体过滤器。
{% assign wordCount = post.content | number_of_words %}
Run Code Online (Sandbox Code Playgroud)
注意:您在帖子标题中有一个奇怪的链接,{{ site.baseurl }}/{{ site.blogs }}{{ post.title }}我认为它{{ site.baseurl }}{{ post.url}}就像在阅读更多链接中一样。
| 归档时间: |
|
| 查看次数: |
1890 次 |
| 最近记录: |