Jump to content

Help for creating Tags please!


Go to solution Solved by MadeMyDay,

Recommended Posts

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 post
Share on other sites
  • Solution

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.

  • Like 6
Link to post
Share on other sites
You MadeMyDay :D 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. 

  • Like 2
Link to post
Share on other sites

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 post
Share on other sites
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 post
Share on other sites

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 post
Share on other sites
  • 1 year later...

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 post
Share on other sites
  • 3 years later...

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! :cool:

(I've put it here for reference for those searching later).

Link to post
Share on other sites

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...

  • Like 2
Link to post
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.

  • Similar Content

    • By Robin S
      Displays image tags overlaid on the thumbnail using customisable colours. This makes it easier to see which images have which tags without needing to open the edit pane for individual images or changing to the list view.
      Screenshot

      Usage
      Enable tags for one or more image fields. Install the Image Thumbnail Tags module. Optionally configure colours for any of your tags.
       
      https://github.com/Toutouwai/ImageThumbnailTags
      https://modules.processwire.com/modules/image-thumbnail-tags/
    • By Andi
      Continuing my journey into PW hooks, I'm trying to find a way to retrieve all images from a page that explicitly *do not* have a certain tag (or tags) attached to them.
      Found this post from 2015
      But I'm wondering if there's a more elegant way to go about this.
      Let's say I have a multi-image field called "images_header" and instead of
      $page->images_header->findTag('mytag'); I would like to do this:
      $page->images_header->excludeTag('mytag'); So I'd be able to do
      // find images that don't have the tag "mytag" $images = $page->images_header->excludeTag('mytag'); // check if there's any images if (count($images)>0) { // do something.. } Would this be possible by hooking into Pagefiles somehow?
      There's this bit in /wire/core/Pagefiles.php Line 626 that I'd basically just need to reverse (or at least in my mind 😄 )
      public function findTag($tag) { $items = $this->makeNew(); foreach($this as $pagefile) { if($pagefile->hasTag($tag)) $items->add($pagefile); } return $items; } Any ideas on how this could be done in a graceful manner?
      Thanks in advance!
    • By Sanyaissues
      After enable the Tags File Compiler module, echoing variables with brackets syntax doesn't work. I try something simple like {page.title} and is displayed like the tag isn't parsed:
      {page.title} This problem happens with PW 3.0.108, i have another installation with 3.0.82 and it works. Any Ideas? thanks.
    • By sww
      Hey there,
      is there really no way to turn the OR logic into an AND logic when selecting pages by (e.g.) tags?
      so instead of
       $pages->find("template=exhibitions, tags=foo|bar") something like
       $pages->find("template=exhibitions, tags=foo&&bar") so the pages needs to have all requested tags, not just any of them.
       
      Thanks,
      Stefan
    • By dimitrios
      Hello,
      I have created a field of type Page Reference and input field type Page Auto Complete, so that users of role 'writer' can add new tags to their articles. However, only a superuser can add new tags through the field, even though 'writer' roles have the permission to create  pages of template 'tag', and the permission to add children in the parent template. New tags in the Page Tree can be added normally. Is there something I am missing?

×
×
  • Create New...