Zahari M. Posted November 11, 2015 Share Posted November 11, 2015 Hi Gents! Question for you... When we do something like this... foreach ($page->images as $image) { blah blah blah } We will be able to get all the images on a page. Kewl But if we wish to use image tags for our images and we tag a couple of images with say the tag "Exclude", how do we write our selector such that all images except those tagged "Exclude" are selected? Many thanks! Regards Zahari Link to comment Share on other sites More sharing options...
SiNNuT Posted November 11, 2015 Share Posted November 11, 2015 http://processwire.com/api/fieldtypes/images/ and then scroll down to 'using image tags'. No direct example of how to find all images except those with a certain tag, but you can use the selector engine so i'm guessing you can use smthin like images.tags!=Exclude You might want or need to do some checks if there are tags present to begin with, i haven't used tags before, but this should get you started. Link to comment Share on other sites More sharing options...
Zahari M. Posted November 11, 2015 Author Share Posted November 11, 2015 Hi SiNNuT Thanks for helping out! Unfortunately I still can't get it to work. I have looked at the documentation where you pointed me to. But as you say, it does not indicate how to exclude any images using a particular tag I have used this as per the documentation to find any images that have a particular tag. In this case my tag is "featured". $images = $page->images->findTag('featured'); I have been trying to write an equivalent selector to the above example that uses an equal sign. If I can do that and select only images that are tagged with 'featured' then hopefully I can try and negate the equals to do what I asked earlier. Here is what I have tried to display any image that has been tagged 'featured'... . But I can't seem to create a selector that works... No images are shown. $images = $page->find("images.tags=featured"); Any other suggestions SiNNuT? Many thanks! Link to comment Share on other sites More sharing options...
Zahari M. Posted November 12, 2015 Author Share Posted November 12, 2015 Ok guys... after digging around and finding something Adrian mentioned elsewhere, this works... $images = $page->images->find("tags!=exclude"); One very important things to take note. This only seems to work when each image has a single tag. If an image has more then one tag, it will not work! Hope that helps! Thanks to SiNNut for the prompt help and of course Adrian! Cheers Link to comment Share on other sites More sharing options...
adrian Posted November 12, 2015 Share Posted November 12, 2015 Zahari - if you need to work with more than one tag, then this approach will work: foreach($page->images as $image) { if(!$image->hasTag("exclude")) echo $image->url; Link to comment Share on other sites More sharing options...
Zahari M. Posted November 12, 2015 Author Share Posted November 12, 2015 Thanks so much Adrian! Ok folks... just for anyone else who stumbles over here.... What I was trying to do was to use a single image field for 3 common things. The usual way would be to create 3 unique image fields. By using tags, i wanted to be more resourceful and keep it simpler by getting away with using just one image field. Now thanks to Adrian and SiNNut I have been able to do just that! What I needed... 1. Upload image/s for articles 2. Upload a single featured image for listings in other pages. Think YouTube video listings with the video thumbnails 3. Upload images to show up in an image gallery. In case 1 above, these images are untagged. In case 2 above, the desired "representative" image for the article is tagged with featured. In case 3 above, any image that is tagged with exclude will NOT show up in the image gallery. Where the problem lied originally was that if an image had more then one tag, I could not filter out what images I did not want. They all appeared! This was because I actually had an image that was both tagged 'featured' and 'exclude'. Adrian's last solution enables me to tag an image with exclude and even if the image has been tagged with something else, it still filters out the image from the gallery. Yay! Here is part of what I did.... it should provide you with enough to get the idea! foreach ($page->images as $image) { if (!$image->hasTag("exclude")) { // Adrian's Magico Any images tagged with exclude will not show up below... $thumbnail = $image->width(320); $content .= "<figure class='block thumbnail' itemprop='associatedMedia' itemscope itemtype='http://schema.org/ImageObject'>"; $content .= "<a href='{$image->url}' itemprop='contentUrl' data-size='{$image->width}x{$image->height}'>"; $content .= "<img src='{$thumbnail->url}' itemprop='thumbnail' alt='{$image->description}'>"; $content .= "</a>"; $content .= "<figcaption itemprop='caption description'>{$image->description}</figcaption>"; $content .= "</figure>"; } } Thanks to the ProcessWire Support team. All of you... World class awesomeness. 3 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