Skip to content
● Grav 2.2 is out: 2x faster cold starts and 96% less memory on big sites. Read the announcement →

Change the page title based on collection type/name

Solved by pamtbaau View solution

Started by Ian 1 year ago · 3 replies · 358 views
1 year ago

I’m new to grav.

I’ve managed to create a new theme based off pure-blind for a blog with tags and an archive sidebar. All working nicely. When I click on a tag, or on an archive link it helpfully takes me to a new page with just the entries in the appropriate collection. However, the page title does not say anything about the filter.

I changed the theme to AntiMatter and it doesn’t seem to have that facility either. I’ve dumped the page variable and I can’t see what anything in that which would help.

Is there an easy way of getting the collection type & details so I can change the page title to something like:

  • Posts from July 2024
  • Posts tagged “Food”
👍 1
1 year ago Solution

@Slartibartfast, You can get the selected tag from the Uri using uri.param():

TWIG
{{ dump(uri) }}               // the Uri object
{{ dump(uri.params) }}        // all parameters
{{ dump(uri.param('tag')) }}  // a single specific parameter => "photography"
{{ dump(uri.param('archives_month')) }}  // a single specific parameter => "aug_2017"

See the docs: Class: \Grav\Common\Uri

👍 1
last edited 08/25/25 by pamtbaau
1 year ago

hello,
let me ask if these are right

  • you have a page that opened like this > https://demo.getgrav.org/blog-skeleton/tag:travel#body-wrapper
  • and you have a title like this > Home | Grav
  • but you want title like this > Home - Travel | Grav

@pamtbaau helped nicely with giving most important part, the code to get date and tag.
After that you can merge them with your page's title by using twig in template like set title = title ~ tag ~ date

1 year ago

Thank you! I’d not spotted the URI class.

For the record I’ve currently settled on

TWIG
    {% if uri.param('tag') %}
    <h1>Posts tagged {{ uri.param('tag') }}</h1>
    {% elseif uri.param('archives_month') %}
    <h1>Posts from {{ uri.param('archives_month') | date('F Y') }}</h>
    {% else %}
    {{ page.content|raw }}
    {% endif %}
👍 1

Suggested topics

Topic Participants Replies Views Activity
General · by Andy Miller, 7 days ago
0 113 7 days ago
General · by lynbor, 1 month ago
3 439 2 weeks ago
General · by lynbor, 3 weeks ago
7 562 2 weeks ago
General · by Harry, 2 weeks ago
0 161 2 weeks ago
General · by Old Man Umby, 1 month ago
2 309 4 weeks ago