Filtering pages and posts by tags that are not in the URL

I am (somewhat) following the guide on filtering content by tags. However, I would like to achieve the following:

In terms of content, I’m working with the starterkit as a base. So I have a projects folder with subpages, and a blog folder with posts. Project pages have a tag and so do blog posts. The markup for a project page looks like this:

Title: Project C
----
Year: 2012
----
Tags: design
----
Text: Lorem ipsum dolor

I have written a controller that gets all the project pages and blog posts and you can filter them with a tag, e.g.: http://localhost:8888/design/tag:design gets all the design projects and pages. I would ideally like to just have a URL like http://localhost:8888/design/.

My controller looks like this. The important bits are lines 13-16:

if($tag = param('tag')) {
    $projects = $projects->filterBy('tags', $tag, ',');
    $blogposts = $blogposts->filterBy('tags', $tag, ',');
}

This gets the tag parameter from the URL. My question is: how do I get the tag from the content?

Could you rephrase this question, please? I don’t quite understand what you mean…:confused:

Hi @texnixe, sorry, I guess I was not clear.

In my project page (project.txt is the filename) there is a field that says Tags: design. Like you can see in my first post.

For a tag page, I am using a template called tag.php. So I have a tag page for design, where the filename is tag.txt and in that file I specify that I would like to display the design tag.

I do this with a field called Tags, in the same way as the project page, but now I realize I should maybe call this TagToDisplay or something like that. Either way, a tag page specifies which projects and posts to display, by setting it as a field.

In the controller, I would like $projects to be the projects that have design as their tag, or whatever other tag should be displayed. I would like the controller to read the tag that should be displayed from the field in the tag page, rather than the URL.

I hope this makes sense!

Now I understand.

Personally, I would do it differently, by using a route instead of pages.

But anyway, in your tag.php controller, you would then not filter by the parameter but by the page field:

$tag = $page->tag(); // or whatever you want to call that field
    $projects = $projects->filterBy('tags', $tag, ',');
    $blogposts = $blogposts->filterBy('tags', $tag, ',');
}

That seems quite simple and elegant.

I’m just wondering: why would you use a route and how would you do it?

I’d use a route because I don’t see why I should create so many pages just to introduce some logic.

See this thread for an alternative solution: Use route to filter out part of directory address in url