This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

hanny13720240
hanny13720240

Is there a quick/easy way to get the first 100 words of a blog article?

I wanted to create a little 'snippet' of the first 100 words of an article to use on another page - with a link to the article after that snippet.

Didn't know if there was some twig shortcut or something to easily snag it.

Shantarli
Shantarli

https://octobercms.com/plugin/vojtasvoboda-twigextensions You can try this plugin that add truncate tag to twig, but its about number of symbols and not words

hanny13720240
hanny13720240

Is there a way to grab a particular article in twig?

Something like {{ article_id | 1 }} or something? If so maybe I can throw the article in a variable then grab the first 100 characters or something.

Shantarli
Shantarli

hanny13720240 said:

Is there a way to grab a particular article in twig?

Something like {{ article_id | 1 }} or something? If so maybe I can throw the article in a variable then grab the first 100 characters or something.

You're using RainLab.Blog plugin, right?

hanny13720240
hanny13720240

Shantarli said:

hanny13720240 said:

Is there a way to grab a particular article in twig?

Something like {{ article_id | 1 }} or something? If so maybe I can throw the article in a variable then grab the first 100 characters or something.

You're using RainLab.Blog plugin, right?

Yes, that's correct.

hanny13720240
hanny13720240

I'm still struggling to do this - so if anyone has any ideas - I'd love to hear them!

Shantarli
Shantarli

hanny13720240 said:

I'm still struggling to do this - so if anyone has any ideas - I'd love to hear them!

Ok, man. Sorry i was away for a few days. Lets dive into the RainLab.Blog plugin. I don't use it by myself but installed it for you. Model "Post" have some fields like: id, title, slug and so on. But it also have an "excerpt" field. I think - this is exactly what you need instead of doing your work with twig extension.

Is there a way to grab a particular article in twig?

Sure. You can execute php code: https://octobercms.com/docs/cms/themes#php-section

Lets stick to this example from docs and be simple.

PHP code section. This code let you take one post model with id=1 and pass it to your page using array notation):

function onStart()
{
    $this['mypost'] = \RainLab\Blog\Models\Post::where('id', 1)->get();
}

Now on the page you can do something dirty and raw:

{%for post in mypost%}
    <h2>{{post.excerpt}}</h2>
    <a href = "/blog/{{post.slug}}">{{post.title}}</a>
{%endfor%}

More

Keep in mind: you can expand every component(blog plugin have 4 of them) by clicking fork button(looks like this http://fontawesome.io/icon/code-fork/) and see what's inside, change something or add something.

hanny13720240
hanny13720240

Awesome!

Thanks for the help - that really did the trick!

Shantarli
Shantarli

hanny13720240 said:

Awesome!

Thanks for the help - that really did the trick!

No problem, mate :)

1-9 of 9

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.