n0sleeves Posted January 16, 2015 Share Posted January 16, 2015 I am having trouble outputting my tagging system correctly. Each music track has a Multi-Page-List-Select field-type which tags associated categories. Tags is a parent page with nested categories beneath. Genre, Mood, Drum Tags each contain additional categories which are ultimately the tags I am associating on the music tracks. In the code below. $genres is where I am having trouble. I think my selector is correct, but my chaining is wrong? // Find Songs $tracks = $pages->get("/Music/")->find("template=track, parent.title!=Drums"); foreach ($tracks as $track) { $track_html = "<li>"; $track_html .= "<a>{$track->title} </a>"; // FIND GENRES EACH TRACK BELONGS TO. $genres = $track->tags->find("parent=genre"); // OUTPUT ONLY GENRES foreach ($genres as $genre) { $track_html .= $genre->title . " "; } $track_html .="</li>"; echo $track_html; } Thank you Link to comment Share on other sites More sharing options...
Jan Romero Posted January 16, 2015 Share Posted January 16, 2015 Interesting approach to tagging You’re trying to specify the parent by name, but I’m pretty sure ProcessWire would much rather have a page ID or a path (meaning, wrapped with forward slashes). Because PW can tell “genre” is not numeric (so not an ID), it treats it like a path, as if the slashes were there, but there are no tags under “/genre/”. They’re under “/tags/genre/”. So try that and it should work. Alternatively, specify that you’re selecting by name: $genres = $track->tags->find("parent.name=genre"); I can see how this can seem confusing, because when if you were looking for a direct child of the homepage, it would work. In that case, the path is the same as the name with slashes. Because you’re not really selecting “dynamic” pages here, but rather pages that are a fixed part of your site’s structure, you might want to use IDs instead. That way, if you later want to change a page name/path, you can do it comfortably in the admin, and don’t have to go around fixing your selectors. 1 Link to comment Share on other sites More sharing options...
LostKobrakai Posted January 17, 2015 Share Posted January 17, 2015 You already have the subpages from the pagefield, so the selector must be has_parent. I don't know if it works with the name / url, but for sure with the id of the parent page. Edit: No need for has_parent. Was a little to eager in posting. Link to comment Share on other sites More sharing options...
n0sleeves Posted January 17, 2015 Author Share Posted January 17, 2015 You’re trying to specify the parent by name, but I’m pretty sure ProcessWire would much rather have a page ID or a path (meaning, wrapped with forward slashes). Because PW can tell “genre” is not numeric (so not an ID), it treats it like a path, as if the slashes were there, but there are no tags under “/genre/”. They’re under “/tags/genre/”. So try that and it should work. Alternatively, specify that you’re selecting by name: $genres = $track->tags->find("parent.name=genre"); I can see how this can seem confusing, because when if you were looking for a direct child of the homepage, it would work. In that case, the path is the same as the name with slashes. Because you’re not really selecting “dynamic” pages here, but rather pages that are a fixed part of your site’s structure, you might want to use IDs instead. That way, if you later want to change a page name/path, you can do it comfortably in the admin, and don’t have to go around fixing your selectors. Providing the full parent path did not work. However, providing either "parent=ID" or "parent.name=genre" did the trick! BTW, great tip / idea on using ID's in case I ever need to change path or names. Thank you so much for the solution! Link to comment Share on other sites More sharing options...
ESRCH Posted January 21, 2015 Share Posted January 21, 2015 Not directly related to your question, but if your tag list is getting too long, it might be useful to activate the "Page Auto Complete" module from Core, which allows you to type in the tags that you want to add, instead of clicking through the list. I find this much easier when I have long lists of tags. Using this module, an approach that I often use for tagging is the following: Create a "tag" template with only a title field Create a "Tags" page under which you will put all the tags Create a "tags" Page field with the following options:Multiple pages allowed Parent of selectable pages: Select the "Tags" page created previously Template of selectable pages: Select the "tag" template Input field type: Select "PageAutoComplete" Check the "Allow new pages to be created from field" option Now, when entering tags, you can type in the name, if it exists already you an select it, otherwise press "Enter" and the tag will be created automatically. 1 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