gtoirog Posted June 27, 2013 Share Posted June 27, 2013 Hi, I just discovered Processwire recently and glad that I found it. I am trying to understand how to create Tags. I know HTML and CSS basics and tiny bit of PHP. I've just read most posts about making tagging system and still confused. Maybe because of my coding skills. From what other people wrote I could understand these: *I should create one field called Tags (this Tags field will be available for posts that i create so that I can type in tag name in it/ or choose from?!?! not quite sure how it will automatically appear in my Tags folder, or maybe I should create each and every tag page under Tags manually) *I should have one template php file called tag.php for each tag page (or two template php files for both Tags parent page(tags.php) and tag children pages(tag.php)) *I should create a Tags page which is the parent for real tag pages So the order I am thinking of to do is like: - create a hidden "Tags" page (which will contain my tag names as pages) under my Home page - assign a template for this page, but not sure what kind of template it will be?!? a template with a php file or without a php file? If with a php file, then what code i shall write in?!? - create a "Tags" field with Type as "Page"; Input -> "Parent of selectable page(s)" as "Tags", "Input field type" -> AsmSelect; "Allow new pages to be created from field?" checked. But i don't know what kind of template to assign for "Template of selectable page(s)" and what code to put inside that template file if it should be a php file. - create one or two php template files. Is that correct? - create my tags under Tags - create a page just to display the list all the tags and if a click on a tag name then i will get a list of all pages with that tag Basically, i think I have no idea about what to write in the template file(s) for the tags. I really want to make a very simple list of tags just for the sake of understanding so I can go from there. Tags will be really necessary for my website. When i looked at the tag.php, tags.php in Ryan's blog, i could not understand very well. Because there were other codes mixed and calling function from blog.inc, main.inc, nav.php. And it totally made me confused as I am not good at coding. Can someone please guide me please, possibly step by step like in the tutorial "Planets" or "Basic website" in Wiki? I really want to learn and I think Processwire is fast and great. Link to comment Share on other sites More sharing options...
MadeMyDay Posted June 27, 2013 Share Posted June 27, 2013 So the order I am thinking of to do is like: - create a hidden "Tags" page (which will contain my tag names as pages) under my Home page - assign a template for this page, but not sure what kind of template it will be?!? a template with a php file or without a php file? If with a php file, then what code i shall write in?!? First of all, create a template called "tags". It is totally fine to leave as it is, just a "title" field, no need for a PHP-file (for now). To be sure, only allow children with template "tags" in the template "family" tab. Then create your hidden "tags" page. Put same tags in it (that means: create children pages with your tags as title) if you like. - create a "Tags" field with Type as "Page"; Input -> "Parent of selectable page(s)" as "Tags", "Input field type" -> AsmSelect; "Allow new pages to be created from field?" checked. But i don't know what kind of template to assign for "Template of selectable page(s)" and what code to put inside that template file if it should be a php file. Correct. Template for selectable pages is "tags". You can also define your hidden tags page as parent. No template file needed (for now). - create one or two php template files. Is that correct? Not necessarily. - create my tags under Tags or from the page select field itself (very convenient). - create a page just to display the list all the tags and if a click on a tag name then i will get a list of all pages with that tag So for that you need to have a .php file as template for the template "tags". First, create your tag list like this: echo "<ul>"; foreach($pages->get('/tags/') as $tag){ //iterate over each tag echo "<li><a href='{$tag->url}'>{$tag->title}</a></li>"; // and generate link to that page } echo "</ul>"; (totally untested, but you'll get it). Since the tag pages have no template yet, this will throw an empty page (or 404, don't know). So let us create a template for the tag pages, which lists all pages with that tag: // Beginning of your template here (head etc.) // select all pages with the tag of this tag page $thisTag = $page->title // Current clicked tag $tagPages = $pages->find("Tags.title=$thisTag"); // select all pages where page field "Tags" contains a page with title of our tag foreach ($tagPages as $tp){ // iterate over the pages with that tag // and generate some teasers (or what you like) echo " <div class='teaser'> <h3>{$tp->title}</h3> <p>{$tp->summary}</p> <a href='{$tp->url}'>Read more...</a> </div>"; } // Rest of your template here (footer, javascripts etc.) as I said, totally untested, but this should point you in the right direction. 6 Link to comment Share on other sites More sharing options...
gtoirog Posted June 27, 2013 Author Share Posted June 27, 2013 You MadeMyDay I followed your advice though I changed some a bit. Here's what I did in details: ~ created a template called "tags" without a (php) file ~ created a template called "tag" without a (php) file * "tags" template -> family: set "Allowed template(s) for children" as "tag" * "tag" template -> family: set "Allowed template(s) for parents" as "tags" ~ created a "tags" field with Type as "Page"; Input -> "Parent of selectable page(s)" as "Tags" (page), "Input field type" -> AsmSelect; "Allow new pages to be created from field?" checked. Set "template for selectable pages" as "tag". ~ created a template called "post" and added "tags" field in addition to its default "title" and "body" fields ~ created a hidden page called "tags" with template "tags" ~ created children of "tags" page as tags with a template "tag" ~ created tags.php file and put the following code in it to render the tags list: <?php include_once("./head.inc"); $tags = $pages->get("/tags/")->children->find("sort=title"); echo "<ul>"; foreach($tags as $tag){ //iterate over each tag echo "<li><a href='{$tag->url}'>{$tag->title}</a></li>"; // and generate link to that page } echo "</ul>"; include("./foot.inc"); ?> ~ created tag.php and put the following code in it to get the list of posts related to the selected tag: $thisTag = $page->title; $tagPages = $pages->find("tags.title=$thisTag"); foreach ($tagPages as $tp){ // iterate over the pages with that tag echo " <div class='teaser'> <h3>{$tp->title}</h3> <p>{$tp->summary}</p> <a href='{$tp->url}'>Read more...</a> </div>"; } include("./foot.inc"); ?> Thank you very much again MadeMyDay and please let me know if there is anything else to improve or fix. 2 Link to comment Share on other sites More sharing options...
gtoirog Posted June 27, 2013 Author Share Posted June 27, 2013 I was also wondering if I can search through the pages selected according to tags? I.e, If I click on a tag and get a list of posts, can I search through these posts selected within a tag? Thanks in advance. i'm liking Processwire Link to comment Share on other sites More sharing options...
MadeMyDay Posted June 27, 2013 Share Posted June 27, 2013 I was also wondering if I can search through the pages selected according to tags? I.e, If I click on a tag and get a list of posts, can I search through these posts selected within a tag? Thanks in advance. i'm liking Processwire Of course. I think there are several possibilities to achieve that (as always in PW). For example you could store the current tag in a session variable and add this variable to the search logic. So for example in your tags.php: $thisTag = $page->title; // you already have that I guess $session->set('currentTag',$thisTag); // store current Tag in session And in your search template (taken from the standard installation) // Search the title, body and sidebar fields for our query text. // Limit the results to 50 pages. // Exclude results that use the 'admin' template. // NEW: Look for tag in session $tagSearch = ""; $currentTag = $session->get('currentTag'); // get tag from session if( $currentTag != '') $tagSearch = ",tags.title=$currentTag"; // add tag to query $matches = $pages->find("title|body|sidebar~=$q, limit=50$tagSearch"); Of course you need to unset the session if the user isn't on a tag page anymore: $session->remove('currentTag'); so put this in your templates which are not tag specific. Link to comment Share on other sites More sharing options...
apeisa Posted June 27, 2013 Share Posted June 27, 2013 If those search results are public, then I would suggest using url param instead of session for tag. It would keep possibility to link search results. Link to comment Share on other sites More sharing options...
MadeMyDay Posted June 27, 2013 Share Posted June 27, 2013 as I said, this is one possible solution ;-) Link to comment Share on other sites More sharing options...
apeisa Posted June 27, 2013 Share Posted June 27, 2013 Yep, and a good one. Mine is merely a little notification about linkability. Link to comment Share on other sites More sharing options...
MadeMyDay Posted June 27, 2013 Share Posted June 27, 2013 Yeah, and a very important notification. Which would lead us to the next possible topic: URLSegments... but one after another ;-) Link to comment Share on other sites More sharing options...
apeisa Posted June 27, 2013 Share Posted June 27, 2013 Or if the search form is on tag page, then no need to look further than $page->name This kind of stuff is definitely where pw really shines. Link to comment Share on other sites More sharing options...
gtoirog Posted June 28, 2013 Author Share Posted June 28, 2013 Thank you so much, guys, for responding fast and being such great helps And I am excited to start my first project in processwire, will probably come back for more answers 2 Link to comment Share on other sites More sharing options...
Can Posted July 20, 2014 Share Posted July 20, 2014 Just implemented tags as well and thought about showing 5 top pages of each tag directly on the tag page So for anyone stumbling across this thread and is curious how this could work. And to extend the code snippets in this forum <?php $content = ''; foreach($tags = $pages->find("parent={$page->id},limit=30") as $t) { $content .= "<h2><a href='{$t->url}'>{$t->title}</a></h2> <strong>5 Top Pages with tag '{$t->title}'</strong> <ul class='tags'>"; foreach($pages->find("tags.title={$t->title},sort=pageviews,limit=5") as $p){ $content .= "<li><a href='{$p->url}'>{$p->title}</a></li>"; } $content .= "</ul>"; } $content .= $tags->renderPager(); this comes into tags.php assuming your tag field is called tags and you have an pageview counter (as discussed here) called pageviews Link to comment Share on other sites More sharing options...
zkriszti Posted December 1, 2017 Share Posted December 1, 2017 For some reason, the Blog Module sample file blog-tag.php gave me an error stating "Error: Call to a member function count() on a non-object (line 830 of C:\wamp\www\...\site\assets\cache\FileCompiler\site\modules\MarkupBlog\MarkupBlog.module) ". With your code above @MadeMyDay, it now works like a charm! (I've put it here for reference for those searching later). Link to comment Share on other sites More sharing options...
SamC Posted December 1, 2017 Share Posted December 1, 2017 I know this is an old thread, but if someone finds this, I also would absolutely recommend this module if you're working with tagging pages: I use it on pwtuts.com and it makes it very easy. In the footer of the aforementioned site, there is a tag list, which is pulled from this file /includes/tags-list.php: <?php namespace ProcessWire; ?> <h2>Tags / <a href="/tags/">See all</a></h2> <ul class="tag-block list-unstyled"> <?php $tags = $pages->get("/tags/")->children; foreach ($tags as $tag): if (count($tag->blogPosts)): ?> <li>#<a href="<?= $tag->url; ?>" class="<?= $tag->name; ?>"><?= str_replace("-", "", $tag->name) ?> (<?= $tag->blogPosts->count ?>)</a></li> <?php endif; endforeach; ?> </ul> ...because the blogPosts fields are connected to the tags, it's just a case of using count() on the tag i.e. if it has page ref fields (blogPosts), print them. Count is used again to output how many posts are associated with each tag. This would have been more awkward for sure without this module, you'd have to get each tag, then find the number of posts that have that tag etc... 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now