Roych Posted June 14, 2017 Share Posted June 14, 2017 Hello, I need some help as I'm not a coder. I've created a slider from repeater which is working great. I need to make some slides with a link so I have created a "Link" field (also working) but now all my slides have links. The one with the new link is linking right but all others link to root. Basicaly what I need is if field is empty show this else this. ..;) I've tried with this: <?php if($page->Slides->Link): ?> <?php foreach($page->Slides as $Slides): ?> <?php // get the very first image from images list since image field is accepting multiple images (unless you change the value of `Maximum files allowed` under Fields > images > Details tab > Maximum files allowed). $image = $Slides->images->first(); ?> <li data-transition="<?php echo $page->Slide_Effect; ?>" data-slotamount="7" data-link="<?=$Slides->Link ?>" data-target="_blank" > <img src="<?php echo $image->url; ?>" alt="<?php echo $image->description; ?>"> <div class="tp-caption" data-x="100" data-y="115" data-speed="700" data-start="1000" data-easing="easeOutBack"><h2><?=$Slides->title ?></h2></div> </li> <?php endforeach; ?> <?php else: ?> <?php foreach($page->Slides as $Slides): ?> <?php // get the very first image from images list since image field is accepting multiple images (unless you change the value of `Maximum files allowed` under Fields > images > Details tab > Maximum files allowed). $image = $Slides->images->first(); ?> <li data-transition="<?php echo $page->Slide_Effect; ?>" data-slotamount="7" > <img src="<?php echo $image->url; ?>" alt="<?php echo $image->description; ?>"> <div class="tp-caption" data-x="100" data-y="115" data-speed="700" data-start="1000" data-easing="easeOutBack"><h2><?=$Slides->title ?></h2></div> </li> <?php endforeach; ?> <?php endif; ?> Not really working. Any help is appreciated. R Link to comment Share on other sites More sharing options...
szabesz Posted June 15, 2017 Share Posted June 15, 2017 (edited) Hi, not tested, but the issue with your code is that you check for empty link before the for loop and not inside of it, so something like this should do: <?php foreach($page->Slides as $Slides): ?> <?php $image = $Slides->images->first(); if ($Slides->Link) { $link = 'data-link="' . $Slides->Link .'"'; } else { $link = ""; } ?> <li data-transition="<?php echo $page->Slide_Effect; ?>" data-slotamount="7" <?= $link ?> data-target="_blank" > <img src="<?php echo $image->url; ?>" alt="<?php echo $image->description; ?>"> <div class="tp-caption" data-x="100" data-y="115" data-speed="700" data-start="1000" data-easing="easeOutBack"><h2><?=$Slides->title ?></h2></div> </li> <?php endforeach; ?> BTW, templates and fields should be named lowercase in the admin, so that you can use lowercase identifiers for your attributes/properties in your code which is a convention for such entities. Edited June 15, 2017 by szabesz typo 1 Link to comment Share on other sites More sharing options...
Roych Posted June 15, 2017 Author Share Posted June 15, 2017 Thank you for reply I tried your suggestion but I get Parse Error: syntax error, unexpected 'endif' (T_ENDIF) ... Error. not sure what is wrong here Thank you R 1 Link to comment Share on other sites More sharing options...
szabesz Posted June 15, 2017 Share Posted June 15, 2017 Oh, sorry for the early morning typo, I left <?php endif; ?> in the example, but <?php endforeach; ?> is needed instead, of course. I'm not good at writing code in the browser, I'm missing my IDE's help 1 Link to comment Share on other sites More sharing options...
Roych Posted June 15, 2017 Author Share Posted June 15, 2017 Yes now it is working great. I got it working with my code also "I think". I used my version of code and put if-else inside like you've said and somehow works. <?php foreach($page->Slides as $Slides): ?> <?php $image = $Slides->images->first(); ?> <?php if ($Slides->Link): ?> <li data-transition="<?php echo $page->Slide_Effect; ?>" data-slotamount="7" data-link="<?=$Slides->Link ?>" data-target="_blank" > <img src="<?php echo $image->url; ?>" alt="<?php echo $image->description; ?>"> <div class="tp-caption" data-x="100" data-y="115" data-speed="700" data-start="1000" data-easing="easeOutBack"><h2><?=$Slides->title ?></h2></div> </li> <?php else: ?> <?php $image = $Slides->images->first(); ?> <li data-transition="<?php echo $page->Slide_Effect; ?>" data-slotamount="7" data-target="_blank" > <img src="<?php echo $image->url; ?>" alt="<?php echo $image->description; ?>"> <div class="tp-caption" data-x="100" data-y="115" data-speed="700" data-start="1000" data-easing="easeOutBack"><h2><?=$Slides->title ?></h2></div> </li> <?php endif; ?> <?php endforeach; ?> I will use your code ofc, shorter and probably better. Thank you very much R 1 Link to comment Share on other sites More sharing options...
szabesz Posted June 15, 2017 Share Posted June 15, 2017 I'm glad you got it working. Just a hint: by duplicating code we make it harder for ourselves to maintain it: http://deviq.com/don-t-repeat-yourself/ I always try to avoid it whenever I have the time to do so (sometimes we just don't...). 2 Link to comment Share on other sites More sharing options...
Roych Posted June 15, 2017 Author Share Posted June 15, 2017 I agree completely. But still learning here, hehe. Thank you R 1 Link to comment Share on other sites More sharing options...
MilenKo Posted June 21, 2017 Share Posted June 21, 2017 <?php foreach($page->Slides as $Slides) $image = $Slides->images->first(); if ($Slides->Link) { ?> <li data-transition="<?php echo $page->Slide_Effect; ?>" data-slotamount="7" data-link="<?=$Slides->Link ?>" data-target="_blank" > <img src="<?php echo $image->url; ?>" alt="<?php echo $image->description; ?>"> <div class="tp-caption" data-x="100" data-y="115" data-speed="700" data-start="1000" data-easing="easeOutBack"><h2><?=$Slides->title ?></h2></div> </li> <?php } else { ?> <li data-transition="<?php echo $page->Slide_Effect; ?>" data-slotamount="7" data-target="_blank" > <img src="<?php echo $image->url; ?>" alt="<?php echo $image->description; ?>"> <div class="tp-caption" data-x="100" data-y="115" data-speed="700" data-start="1000" data-easing="easeOutBack"><h2><?=$Slides->title ?></h2></div> </li> <? }; }?> Something like this might make it shorter. Please excuse me if I forgot to close a { or else as the browser is not my best editor. Basically I removed $image defininition in your second case as it is already defined before the if and is not changing. 1 Link to comment Share on other sites More sharing options...
MilenKo Posted June 21, 2017 Share Posted June 21, 2017 If you want even shorter version of your code, you can remove completely the repeating lines and just leave the differences as @szabesz suggested earlier: <?php foreach($page->Slides as $Slides) { $image = $Slides->images->first(); if ($Slides->Link) { $link = $slides->Link; } else { $link = ""; }; echo "<li data-transition='$page->Slide_Effect' data-slotamount='7' data-link='$link' data-target='_blank' >"; echo "<img src='$image->url' alt='$image->description'>"; echo "<div class='tp-caption' data-x='100' data-y='115' data-speed='700' data-start='1000' data-easing='easeOutBack'><h2>$Slides->title</h2></div>"; echo "</li>"; } ?> Hope that helps 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