How to check if a tag has been selected - then do something
Permalink
In need to display a video icon for any pages that have had a videos tag selected - basically I need to add a <span> into the markup if the videos tag has been selected:
At the start of he foreach loop I have this to load the tags:
Then in the markup I'm trying to evaluate if the 'videos' tag has been selected like this
As you can tell, I really understand how to check if the 'videos' tag has been selected - or if I'm setting up everything correctly.
Also, do you need an else statement incase the video tag hasn't been set?
Any pointers in the right direction would be much appreciated.
At the start of he foreach loop I have this to load the tags:
$tags = $page->getCollectionAttributeValue("recipe_tags"); // attribute handle of tags to display
Then in the markup I'm trying to evaluate if the 'videos' tag has been selected like this
<?php // if an image is set, output if ($img) { ?> <p> <a href="<?php echo $url ?>" target="<?php echo $target ?>"> <img src="<?php echo $thumb->src ?>" width="<?php echo $thumb->width ?>" height="<?php echo $thumb->height ?>" alt="<?php echo $title ?>" /> <?php // SEARCH TAGS FOR A SPECIFIC TAG $videoTag = $tags('Videos'); if ($videoTag) { ?> <span></span> <? } ?> </a> </p> <? } ?>
As you can tell, I really understand how to check if the 'videos' tag has been selected - or if I'm setting up everything correctly.
Also, do you need an else statement incase the video tag hasn't been set?
Any pointers in the right direction would be much appreciated.
![](/files/avatars/none.gif)
Removed to make thread easier to follow.
Removed to make thread easier to follow.
Removed to make thread easier to follow.
Removed to make thread easier to follow.
Removed to make thread easier to follow.
Removed to make thread easier to follow.
Removed to make thread easier to follow.
Removed to make thread easier to follow.
<?php // SEARCH TAGS FOR A SPECIFIC TAG $tags = CollectionAttributeKey::getByHandle('recipe_tags'); // attribute handle of tags to search $tagToFind = 'Videos'; // declare the specific tag to find $selectedTags = array($page->getAttribute($tags->getAttributeKeyHandle())); // get tags associated with each page and put them in an array foreach ($selectedTags as $tag) { // separate selected tags out of the array echo $tag; // ECHO TEST - output individual tags associated with each page if($tag == $tagToFind) { // if $tag is equal to $tagToFind echo ' Found'; } else { echo ' Not found'; } } ?>
Based on the code above (echo test on), the tags for each page are being listed - along with 'Video not found' like this:
Dinner Easy Lunch Quick Meals Supper Videos Not found
As you can see, some of the results show the Videos tag but I'm obviously searching or evaluating incorrectly because each result outputs 'Videos not found' - despite the Videos tag being clearly present in some of the page results.
Any pointers in the right direction would be welcome at this point - I'm going crazy trying to figure this out alone!
Removed to make thread easier to follow.
Should something simpler like this (from the php in_array manual) work in c5?
<?php // SEARCH TAGS FOR A SPECIFIC TAG $tags = CollectionAttributeKey::getByHandle('recipe_tags'); // attribute handle of tags to search $tagToFind = 'Videos'; // declare the specific tag to find $selectedTags = array($page->getAttribute($tags->getAttributeKeyHandle())); // get tags associated with each $page and put them in an array if (in_array($tagToFind, $selectedTags, true)) { // search the array for $tagToFind - true = strict echo 'Video!'; } else { echo 'No Video!'; } ?>
Removed to make thread easier to follow.
Even the smallest hint would be helpful at this point!
I removed a bunch of rambling posts to try and make the thread easier to follow as well.
I'm sure I'm missing something basic so any pointers in the right direction would be much appreciated.
I removed a bunch of rambling posts to try and make the thread easier to follow as well.
I'm sure I'm missing something basic so any pointers in the right direction would be much appreciated.
OK, so this works.
<?php // use tags set to output <span> if recipe is a video (to display video icon) $tags = CollectionAttributeKey::getByHandle('recipe_tags'); // attribute handle of tags to use $tagToFind = 'Videos'; // the specific tag to search for $selectedTags = $page->getAttribute($tags->getAttributeKeyHandle()); // get tags associated with each // search $selectedTags for $tagToFind if (stristr($selectedTags, $tagToFind)) { ?> <span></span> <? } ?>