Skip to content
● Meet Grav Guard: keep every Grav site you run secure and up to date, all from one place. Free for up to 3 sites. Read more →

Page Objects & YAML Front Matter

first-time

Solved by Paul Massendari View solution

Started by Mason 9 years ago · 3 replies · 1090 views
9 years ago

Hi,
My code for post-item.html.twig can be found here

So I am attempting to show the first image in the blog post on the screen above, hence, I have included {% set blog_image= page.media.images|first %}. However, I am unsure of how to call on this in the <img src="">, and no image is showing up.

Moreover, it seems that there is something wrong with my YAML Front Matter as I specify the Page Author {{ page.header.author }}, however when it displays on the site (as shown in the image) it shows nothing, despite me adding an author in the post.md

Any help would be fantastic!

9 years ago

Due to image restrictions, here is the information I wish to change.
image|690x366

9 years ago Solution

as you are in a for loop, you have to use the variable you defined, in this case post.

Example:

TWIG
{% for post in page.children %}
{{post.title }}
{{ post.header.author }}
{{ post.media.images|first }} {# will display the img tag #}
<img src="{{ post.media.images|first.url}}"
{% endfor %}

Also you have some kind of weird syntax here:

TXT
<img src="$blog_image.url" alt=""/>

Where does this come from?

Hope it helps,

👍 1
9 years ago

@paul:
as you are in a for loop, you have to use the variable you defined, in this case post.

Example:

TWIG
{% for post in page.children %}
{{post.title }}
{{ post.header.author }}
{{ post.media.images|first }} {# will display the img tag #}
<img src="{{ post.media.images|first.url}}"
{% endfor %}

Also you have some kind of weird syntax here:

TXT
<img src="$blog_image.url" alt=""/>

Where does this come from?

Hope it helps,

Ahh Perfect! Thank you for that! You've fixed all my problems!
Cheers.

last edited 03/23/18 by Mason

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by MarkP, 2 weeks ago
3 193 2 weeks ago
Themes & Styling · by TomW, 2 weeks ago
0 111 2 weeks ago
Themes & Styling · by themetman, 3 weeks ago
1 206 3 weeks ago
Themes & Styling · by AH, 1 month ago
1 324 3 weeks ago
Themes & Styling · by Bruno, 4 years ago
6 1523 3 weeks ago