Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Add Author name in blog post?

Started by Muut Archive 11 years ago · 18 replies · 2603 views
11 years ago

Hello I have just tried GRAV and found it extremely powerful and easy to use.
I just want to add author name, picture and some info at the end of a page.

11 years ago

You can add those things in the page header frontmatter, then you will have them available to access via the twig. If it's an author you use frequently, you could put that information in the site.yaml, so you dont have to repeat it per page:

/user/config/site.yaml

YAML
author:
  andym:
      name: Andy Miller
      email: a[email protected]
      avatar: /images/andym.jpg
      bio: some short biography here

Then in your twig you have modified to show the author you can access the bits like this:

TWIG
<b>Author Name: {{ site.author.andym.name }}</b>
11 years ago

Its bit confussing, but if I got you right I just have to paste the code in the ".md" file, Right? File Manager

11 years ago

There are two ways for you you should consider. First you can paste the above code directly in your ".md" file, but then you explicitely have to enable Twig in the page header via

YAML
process:
    twig: true

(see [Learn Grav -> Headers](http://learn.getgrav.org/content/headers#process for the documentation) ). However you have to repeat this (set twig in header + insert code in ".md" file) for every ".md file" you want to show the author. In this case the second option might be more interesting for you (however for testing purposes the first approach is perfect).

It runs as follows: you modify the Twig file of your template, i.e. if you have Antimatter installed you modify user/templates/partials/blog_item.html.twig somewhere around Line 31 and add

TWIG
<b>Author Name: {{ config.site.author.andym.name }}</b>

to the code.

Since you are modi fying a template then you probably want to consider template inheritance, too, to keep your changes after updating your base theme(s).

11 years ago

hmmm. {{ site... }} should work fine also. It is a shortcut to config.site, but should still work, if it doesn't can someone ticket that as an issue?

11 years ago
YAML
{{ site.author.andym.name }}

This works. I just tested it with newest grav so it's not an issue.

11 years ago

Maybe you have other "author" values in site.yaml and they are overriding each other ? Author it's often there by default.

11 years ago

Hi @rhukster, maybe you both are right. I don't have a machine here to test*. I guess the OP is missing the page header process: twig: true in the "md" file, which will solve the problem. However, if {{ site.author.andym.name }} works, the documentation has to be updated, though.

  • I will test it, when I'm back at home and leave a message here, when this is not working.
11 years ago

Ok, the documentation said it, it's an alias. But it is a little confusing for me. An example would be fine.

11 years ago

Example is in my above link :) It's just not in site object :)

11 years ago

I tried all but no luck, can someone provide me complete files in which edits are made, then it would be a lot more easier for me?

11 years ago

Oh wait a minute, I just tried the step mention by the 'admin' on GRAV defualt install and it worked like a chram, but its not working on skeleton pacakge 'Blog Site' in which I was previously trying. Can anyone confirm that this can be done in 'Blog Site' ?

11 years ago

This can be done in 'Blog Site'. Where exactly are you pasting the code ? Maybe you can send me whole skeleton using dropbox or gitter ? or at least paste code from a file here ?

11 years ago

Here's the blog post:

YAML
title: The Urban Jungle
date: 17:34 07/04/2014 
taxonomy:
    category: blog
    tag: [travel, photography, city]
    author: andym
    process: 
    twig: true

xyz         
{{ site.author.andym.name }}

and here's site.yaml file:

YAML

title: Grav
author:
  name: Andy Miller
  email: '[email protected]'
blog:
  route: '' 
metadata:
    description: 'Grav is an easy to use, yet powerful, open source flat-file CMS'
taxonomies: [category,tag,month]
11 years ago

Your declaring those details inside of taxonomy which is incorrect.

Try something like below

YAML

title: The Urban Jungle
date: 17:34 07/04/2014
author: andym
process:
    twig: true
taxonomy:
    category: blog
    tag: [travel, photography, city]
---
11 years ago

Durlabh this makes no sense.
{{ site.author.andym.name }} should be placed INSIDE twig template. So if your page files is called item.md then corresponding twig template file should be: item.html.twig. Then for example after

TWIG
<div class="blog-content-item grid pure-g-r">

you can paste your code: {{ site.author.andym.name }}
And author will show for all blog items. :)

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1327 9 years ago
Archive · by Muut Archive, 9 years ago
2 921 9 years ago
Archive · by Muut Archive, 9 years ago
2 4050 9 years ago
Archive · by Muut Archive, 9 years ago
1 2928 9 years ago
Archive · by Muut Archive, 9 years ago
3 1107 9 years ago