Jump to content

Find all pages matching tag (using urlSegment)


a-ok
 Share

Recommended Posts

If I am implementing a 'free tagging field', which is explained by @ryan here > https://processwire.com/talk/topic/71-categorizingtagging-content/ is there any way to do this for more than one word (for example if a tag I entered is 'green energy')

So, I have a tag.php template which uses something like:

<?php if ( $tag = $sanitizer->selectorValue($input->urlSegment1) ) : ?>
<?php $matches = $pages->find("tags*=$tag"); ?>

<?php foreach ($matches as $match) : ?>
<?php endforeach; ?>

<?php endif; ?>

This allows me, using url segments, to do: /tags/green and the results with a tag 'green', would come up. But how would I do it for something like 'green energy', so /tags/green+energy or /tags/green-energy...

Currently my free tagging field is just a text field, but I could make this into a repeater, for example, if it helps this situation.

Thanks in advance

post-2553-0-31957600-1417803020_thumb.pn

Link to comment
Share on other sites

You could use an autocomplete pageField for the tags. That way existing tags are available across all pages using the field.

Check the "allow new pages to be created from this field" option.

The end result, is that your tags are now page references that you can search against by the sanitized name field.

  • Like 2
Link to comment
Share on other sites

Hi @renobird, thanks for the help and sorry for the slow response.

I don't quite think I understand... essentially I'm wondering if you can search by tag if the 'tag' is more than one word. So, if it's one word, http://www.domain.com/tags/green, for example, works... but if it's two or more, I'm not sure how to word it http://www.domain.com/tags/green-energy or http://www.domain.com/tags/green+energy etc.

I think I kind of understand, but couldn't find the 'allow new pages to be created from this field' checkbox.

Thanks in advance.

Link to comment
Share on other sites

Hi Richard,

If you use the pageField method, then every "tag" creates a page that has a unique name. Example (green-energy, shark-sandwiches, turtle-soup).

The autocomplete field presents the user with the page title, so they would select tags like (Green Energy, Shark Sandwiches, Turtle Soup). Same applies for adding new tags.

Now you can use UrlSegments to find pages with your multi-word tags because they will be in a format like /domain/tags/green-energy/

  • Like 4
Link to comment
Share on other sites

@renobird Hi, sorry... quick thing... if I originally used:

<?php if ( $tag = $sanitizer->selectorValue($input->urlSegment1) ) : ?>
<?php $matches = $pages->find("tags*=$tag"); ?>

How would I use urlSegment1 for this new setup you suggested? I thought this might've worked...

<?php if ( $tag = $sanitizer->pageName($input->urlSegment1) ) : ?>
<?php $matches = $pages->find('/tags/$tag/'); ?>

Many thanks...

Link to comment
Share on other sites

@Jan Thanks for that... makes sense! :) Unfortunately this doesn't seem to solve the issue as

<?php if ( $tag = $sanitizer->pageName($input->urlSegment1) ) : ?>

doesn't seem to be returning anything on /tags/green-energy for example...

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
 Share

×
×
  • Create New...