Jump to content

Check if image has tags saved


suntrop
 Share

Recommended Posts

The documentation is a little short on this topic … there seems to be only a getTag() and findTag()

No way to check if there is a tag at all? The hasTag() method seems to be not allowed on image fields.

I want to use an existing image field (something like WordPress’s featured image/post thumbnail) to gather images for a gallery as well.

Problem is …

<?php if(count($page->images)): ?>
	<img class="pull-right thumbnail" src="<?php echo $page->images->first->height(300)->url ?>" alt="">
<?php endif; ?>

… there are thousands of pages/images without any tags and I can’t tag them all with »featured« to use them as now. Thus I have to tell my template to only use the first image without a tag to use as the featured image.

Sounds complicated :-) Hope you know what situation I am in 

Link to comment
Share on other sites

$tagless_images = new WireArray();
foreach ($page->images as $image) {
    if(empty($image->tags)) {
        $tagless_images->add($image);
    }
}

Now $tagless_images is a WireArray of images on the page that have no tag.

Edit: this is better...

$tagless_images = $page->images->filter("tags=''");
  • Like 2
Link to comment
Share on other sites

Thanks. It works :-)

The filter method looks far better, but I need the tagged photos later on this page.
I tried this one 

<?php if(count( $page->images->has("tags=''") )): ?>

but it is always true, even if there are only images with tags.

Link to comment
Share on other sites

The filter method looks far better, but I need the tagged photos later on this page.

Not sure what you mean; $tagless_images are your images without tags, for images with tags you use:

$mytag_images = $page->images->findTag('mytag');

I tried this one 

<?php if(count( $page->images->has("tags=''") )): ?>

but it is always true, even if there are only images with tags.

Count does not belong in your if expression: ->has() returns true or false so it doesn't make sense to count it.

  • Like 2
Link to comment
Share on other sites

  • 3 weeks later...

Not sure what you mean; $tagless_images are your images without tags, for images with tags you use:

filter() strips out all images without a tag. Those are not in $page->images after using filter()

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...