Jump to content
a-ok

Find all pages matching tag (using urlSegment)

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

Share this post


Link to post
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

Share this post


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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

@renobird Ah right yes, I get you. I started writing a reply... but then I suddenly got it. Okay great... I will try this. Most appreciated!

Share this post


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

Share this post


Link to post
Share on other sites

$variables are only resolved when inside "double quotes". With the 'single quotes', $pages->find() will always look for /tags/$tag/, never /tag/green-energy/, etc..

Might just be a typo in your post, but I thought I’d mention it.

Share this post


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

Share this post


Link to post
Share on other sites

Worked it out...

<?php $tag = $page->name; ?>
<?php $matches = $pages->find("tags=$tag"); ?>
<?php foreach ($matches as $match) : ?>
  • Like 2

Share this post


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 Robin S
      A new module that hasn't had a lot of testing yet. Please do your own testing before deploying on any production website.
      Custom Paths
      Allows any page to have a custom path/URL.
      Screenshot

      Usage
      The module creates a field named custom_path on install. Add the custom_path field to the template of any page you want to set a custom path for. Whatever path is entered into this field determines the path and URL of the page ($page->path and $page->url). Page numbers and URL segments are supported if these are enabled for the template, and previous custom paths are managed by PagePathHistory if that module is installed.
      The custom_path field appears on the Settings tab in Page Edit by default but there is an option in the module configuration to disable this if you want to position the field among the other template fields.
      If the custom_path field is populated for a page it should be a path that is relative to the site root and that starts with a forward slash. The module prevents the same custom path being set for more than one page.
      The custom_path value takes precedence over any ProcessWire path. You can even override the Home page by setting a custom path of "/" for a page.
      It is highly recommended to set access controls on the custom_path field so that only privileged roles can edit it: superuser-only is recommended.
      It is up to the user to set and maintain suitable custom paths for any pages where the module is in use. Make sure your custom paths are compatible with ProcessWire's $config and .htaccess settings, and if you are basing the custom path on the names of parent pages you will probably want to have a strategy for updating custom paths if parent pages are renamed or moved.
      Example hooks to Pages::saveReady
      You might want to use a Pages::saveReady hook to automatically set the custom path for some pages. Below are a couple of examples.
      1. In this example the start of the custom path is fixed but the end of the path will update dynamically according to the name of the page:
      $pages->addHookAfter('saveReady', function(HookEvent $event) { $page = $event->arguments(0); if($page->template == 'my_template') { $page->custom_path = "/some-custom/path-segments/$page->name/"; } }); 2. The Custom Paths module adds a new Page::realPath method/property that can be used to get the "real" ProcessWire path to a page that might have a custom path set. In this example the custom path for news items is derived from the real ProcessWire path but a parent named "news-items" is removed:
      $pages->addHookAfter('saveReady', function(HookEvent $event) { $page = $event->arguments(0); if($page->template == 'news_item') { $page->custom_path = str_replace('/news-items/', '/', $page->realPath); } }); Caveats
      The custom paths will be used automatically for links created in CKEditor fields, but if you have the "link abstraction" option enabled for CKEditor fields (Details > Markup/HTML (Content Type) > HTML Options) then you will see notices from MarkupQA warning you that it is unable to resolve the links.
      Installation
      Install the Custom Paths module.
      Uninstallation
      The custom_path field is not automatically deleted when the module is uninstalled. You can delete it manually if the field is no longer needed.
       
      https://github.com/Toutouwai/CustomPaths
      https://modules.processwire.com/modules/custom-paths/
    • 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 AndZyk
      Hello,
      I am currently building a intranet which will be hosted on the local network of a company. This intranet has many links to files on their fileserver with the protocol file://.
      So for example the links look like this file://domain.tld/filename.ext
      When I try to insert such a link into a URL field, I get the error, that only the protocol http:// is allowed. When I try to insert such a link into a CKEeditor link, it gets stripped out. Is it possible to insert such links into the FieldType URL and CKEditor.
      I know that I could use a FieldType Text or insert a RewriteRule in the .htaccess file, but I am looking for a more elegant solution. 😉
      Regards, Andreas
    • By stanoliver
      Hi! 
      The following code snippet is part of my markup simple navigation and the url_redirect (url field in the backend) just works fine when I put an special custom url into the url_redirect field.
      <?php $nav = $modules->get("MarkupSimpleNavigation"); // topnav $current = $page->rootParent(); // current root page // subnav echo $nav->render(array( 'max_levels' => 2, 'item_tpl' => '<h4><a class="g8-bar-item g8-hover-black" href="{url_redirect|url}">{title}</a></h4><hr class="sidenav">', 'collapsed' => true, ), $page, $current); ?>  In my seperated breadcrumb navigation I use the following code snippet
      <?php foreach($page->parents()->append($page) as $parent) { echo "<li><a href='{$parent->url_redirect|url}'>{$parent->title}</a></li>"; } ?> Now to the problem: In my first code snippet above the
      url_redirect|url 
      works just fine but when I try something similiar in the second code snippet
      $parent->url_redirect|url
      I produce an server error How do I have to change the second code snippet so that it works in the correct way as the first code snippet does?
×
×
  • Create New...