Limit the {{ .Summary }}

Hi,

I’m new at Hugo, and I’m in the mid of porting all my site from wordpress to Hugo.
Is there any ways to limit the words of summaries ?

As the default is first 70 words, how if I want to make it less than that ?
The problem I faced right now is I the content from wordpress more tag is more than 70 words.

Cheers,
Raff

Use <!--more--> in your content file. For more information look here.

Hi,

Thanks for your help, yes I already have <!--more--> tag from my previous exported wordpress post but the length are more than 70 words.

Seems like I didn’t have any ways to modify all my 1700 posts to make it short.

Cheers,
Raff

Haven’t done this myself, but how about using the string functions? It doesn’t seem possible to subtract the first 70 words from a string, but you can extract a certain amount of characters from the beginning of .Content it seems with slicestr.

Never tried this before, but lets see how far I can go with this.

Thanks for your help.

Cheers,
Raff

Update

Okay, I managed to reduce the at least the text into 200 chars by:
{{slicestr .Summary 0 200}}

So far I’m happy with the result for now.
But let me know if you guys have another better approach to make excerpts.

Thanks for the help.

Cheers,
Raff

Second update

There is no issues on the display but I’ve got an error link this:
ERROR: 2016/09/16 14:26:32 template.go:131: template: theme/partials/blog.html:39:34: executing "theme/partials/blog.html" at <slicestr .Summary 3 ...>: error calling slicestr: slice bounds out of range in theme/partials/blog.html

What would that be ?

While on the code:
<!--DETAILS--> <div class="content"> <ol class="breadcrumb"> {{ if isset .Params "categories" }} {{ if gt (len .Params.categories) 0 }} in <a href="{{ $.Site.BaseURL }}categories/{{ index .Params.categories 0 | urlize | lower }}">{{ index .Params.categories 0 }}</a></li> {{ end }} {{ end }} </ol> <h6><a href="{{ .Permalink }}">{{ .Title }}</a></h6> <p> {{slicestr .Summary 3 200}} </p> <a href="{{ .Permalink }}">Read More</a> </div>

Perhaps the .Summary of one of the posts was not 200 characters (an empty post), and with slicestr you tried to get the first 200 characters (which weren’t there).

If you check whether a post has a .Summary of at least 200 characters, does the error then go away?

For example (untested):

{{ if gt (len .Summary) 200 }}

  <h6><a href="{{ .Permalink }}">{{ .Title }}</a></h6>
  
  <p>{{slicestr .Summary 3 200}}</p>
  
  <a href="{{ .Permalink }}">Read More</a>

{{ end }}
2 Likes

Works like a Charm,

Thanks a lot for your help, much appreciated.

Cheers,
Raff

You want to use this:

{{slicestr .Summary 3 200 | markdownify }}

Otherwise your summary show markdown code/formatting and won’t render it.

Cheers! :slight_smile:

Hi!
Where should i put this?

Thanks!

You can set a new default:

summarylength = 30

Also there is a truncate template func that does what it says.

4 Likes

Hi,bep.
I can set a new default:

summarylength = 10

However, it is invalid for Chinese.

Use hasCJKLanguage = true solved it