Jump to content

List of post with same tag


Recommended Posts



Sorry to bother again with my Noobies questions but I can't figure out something, It's probably have treated 100 times but as I probably don't have the good vocabulary I can't find anything in the forum.

I would like to have a page who sort all my post ( they all have the same template named project) that share a same tag or category ( tag and category are two fields of page reference in the project's template )

so far I'm here

		$items = $pages ->get("template=project")->children("tags=$page");
				foreach ($items as $item){
										echo <li>$item->title</li>;
										} ?>

Also should I repeat the code with category field or is it better to have two separate templates one for the category list and one for the tag.

I appreciate a lot all the help you can give me.

my best

Link to comment
Share on other sites

If I understand you correctly, you want every individual project page to list all other project pages that has the same tag? If yes, then the following code could work:

$items = $pages->find("template=project, tags=$page");

Just to expand further on that: In your code line, if your code would work, would return something like a random project page's child pages of any template that is tagged with $page (current page). But then, ->children is supposed to be used with $page, not $pages.

Link to comment
Share on other sites

Thank you very much Snobjorn, it works as a charm.

Is there a function that retrieve me the type of field so it can change from tags to categories.

$items = $pages->find("template=project, "function to find if $page is a tag reference or category" =$page");

the idea is to have only one template that I could use for listing all the project of one category or all the project that shares one tag.

Link to comment
Share on other sites

To find the template for the page you are currently at ($page), you can use the information with $page->template. Then you can use a if/else statement to set a variable that would be the field name.

if ($page->template->name == "tag") {
	$field_name = "tag";
} elseif ($page->template->name == "category") {
	$field_name = "category";

$items = $pages->find("template=project, $field_name=$page");

It might be other ways as well, but this should be good enough.

Link to comment
Share on other sites

Thank you very much for all your help Snobjorn.

This code doesn't work because i'm trying to have the same template for tag and category.

but is this code would be correct?

if ($page->parent-> template->name == "tags") {
	$field_name = "tag";

in that case I would make a template for categories and for tags and have the same template for both childrens

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...