Creating a blog page thats using just tag items

Hi,

I wanted to create a page that lists blog items that are of a specific tag.

I can see how to use blog.html.twig is doing that in users/themes/templates - what I cant figure out is how I get a root level page to be able to retrieve blog entries when its not associated with the blog?

My blog is my index page and then there are a couple of default pages - I want my blog-tag page to appear in the nav with these.

many thanks

Not sure I understood what you want. Is it the list of pages with specific tag or a navigation item to one of the default pages? Could you post your pages structure with taxonomies and what exactly you want the result to be?

OK, sure I can see thats not a good explanation.

Im struggling to explain this. If I do a page structure its not going to help, as it will look like I just want a new default page.

What about this, if this below is the basic website with blog pages structure

/user
└── /pages
├── /01.blog
│ ├── /blog-item-1
│ ├── /blog-item-2
│ ├── /blog-item-3
├── /02.page 1
├── /03.page 2
└── /error

In theory I could alter the page /01.blog with the template blog.html.twig, and I could make it just display items with the tag ‘yellow’ right? I would have a new edited version of /01.blog

Now suppose I wanted to use both the unedited and edited versions of /01.blog? so my blog could both display all my blog posts as usual and on another blog page it could display only the posts with the ‘yellow’ tag.

Now suppose I want in our file structure /03. page 2, which is a flat default page to in fact show what the edited version of the blog posts is?

Is this possible?

And I suppose a common sense explanation is that I have a blog currently with 4 main topics - this will expand. I want a page that has posts just from one topic - as its more important.

Im my case I have the topics design, building, apple, motorbikes - and I want a dedicated page for my most important topic - design.

@louigi, It sounds like you want a collection containing all pages with a certain tag. Right?

If so, have a look at Taxonomy Collections.

You can use the collection to list all pages in a ‘blog’ like page, but you can also use a collection to create a navbar.

Ok, sure that makes sense - this code I can see is what I want,

content:
   items:
      '@taxonomy.tag': design

But the confusing bit is how thats applied. Ive got my default page in the root. I can see its going in the .md file right? then which template is it using to display?

Whichever the file name matches. default.md uses default.html.twig, form.md uses form.html.twig. You can actually use same template to list different items.

@louigi, It will use the template as defined by the name of the page in which the collection is defined or it will use the variable ‘template’ in the page’s frontmatter.

Looking at your folder structure, I presume that will be template ‘blog.html.twig’.
Template ‘blog.html.twig’ is designed to use the collection defined in the frontmatter of a page and list the items in the collection.

If you are using a template that does not make use of a collection, you will have to update an existing template or create a new template similar to ‘blog.html.twig’

You can also create a collection inside Twig in any template (see docs on collections):

{% set collection = page.evaluate([{'@page.children':'/blog', '@taxonomy.tag':'photography'}]) %}
{% set ordered_collection = collection.order('date','desc') %}

OK, so forgive me here - Im not a developer, so some of it is a bit of a reach.

I can basically duplicate a template or make a new one with as you suggest using {% set collection = page.evaluate([{’@page.children’:’/blog’, ‘@taxonomy.tag’:‘photography’}]) %}

Then a page using the same name - and by page we mean a folder with the same name containing a .md file with the same name? and this file contains various switches to the template and other html and so on? Have I got that about right?

I took a look through the code for blog.html.twig. It seems to me this is the critical bit of code iterating through the blog pages and displaying

{% embed 'partials/layout.html.twig' with {blog: page} %}
   {% block item %}

      <div class="bricklayer">
        {% for child in collection %}
           {% include 'partials/blog-list-item.html.twig' with {blog: page, page: child} %}
        {% endfor %}
      </div>

Whats a bit difficult to work out here is the interaction between

{% set blog = page.find(header_var('blog_url')|defined(theme_var('blog-page'))) %}

And

{% set collection = page.collection() %}

How does the ‘collection’ and ‘blog’ relate?

Im going to close this topic myself.

The basic problem was that I wasn’t updating the cache so the resultant page wasn’t reflecting changes in the template. its annoying to be stuck on that singular problem for quite a while when the solution was very simple.

As stated @pamtbaau - what you were suggesting is exactly right, the use of a custom template allowed me to use the code you suggested - although I changed it a bit, but its working just fine now.

Thanks for the help.