OllieMackJames Posted March 28, 2013 Share Posted March 28, 2013 I am trying the following: I have added a structure to my site: - Sidebar Ads - Side Ad Number One - Side Ad Number Two These Side Ads have the folllowing fields: - title - page_url (url of site I want to link to) - page_target_blank - meta_description - images In sidebars I specify per page which ad to show, through another added field called: SidebarAds I am now using the following code in the article template, which seems to almost work, only problem is that I can not see an image. <?phpif(count($page->SidebarAds) > 0) { if($page->id != 1) { echo '<div class="col_12 sidebar">'; }?><h3 class="col_12 header">NEW Self Help Program</h3> <ul class="related col_12"> <?php foreach ($page->SidebarAds as $sidebarAd) { $target = ($sidebarAd->page_target_blank == 1 ? ' target="_blank"' : ''); echo "<li class='col_12'><a href='{$sidebarAd->page_url}'{$target}><img src='{$sidebarAd->images}'alt='$sidebarAd->title.' width='400' height='289'></a></li>"; } ?> </ul><?php if($page->id != 1) { echo '</div>';}}?> If I look in the source, the source shows: src="name-of-image.jpg" without the proper url to make it show up. I hope this makes sense, so the question is how to change the code so it shows the image correct. Thanks! Link to comment Share on other sites More sharing options...
Manfred62 Posted March 28, 2013 Share Posted March 28, 2013 think, should be ... $sidebarAd->images->url 1 Link to comment Share on other sites More sharing options...
diogo Posted March 28, 2013 Share Posted March 28, 2013 You have to correct this: img src='{$sidebarAd->images->url} edit: Manfred had faster fingers Link to comment Share on other sites More sharing options...
OllieMackJames Posted March 28, 2013 Author Share Posted March 28, 2013 Thanks Manfred62 and diogo, it is getting closer, but still not there, now it returns me: src="/site/assets/files/1097/" so I figured the following might work: <img src='{$sidebarAd->images->url}{$sidebarAd->images}'alt='$sidebarAd->title.' width='400' height='289'> and that did the trick. Thanks for thinking along, I do not fully understand why it can not get it in one go, but this seems to work. Link to comment Share on other sites More sharing options...
Manfred62 Posted March 28, 2013 Share Posted March 28, 2013 not sure, but in the API there's only image, not images? $sidebarAd->image->url Link to comment Share on other sites More sharing options...
OllieMackJames Posted March 28, 2013 Author Share Posted March 28, 2013 not sure, but in the API there's only image, not images? $sidebarAd->image->url images is the name of the field that is used I tried your suggestion as well, but that did not work. src='{$sidebarAd->images->url}{$sidebarAd->images} does work, would like to know how to get this in one go still though. Thanks Link to comment Share on other sites More sharing options...
diogo Posted March 28, 2013 Share Posted March 28, 2013 If your field doesn't limit the amount of images to one, you have to do this: src='{$sidebarAd->images->first->url} but if you are planning to have only one image on that field anyway, the correct thing would be to define tha limit on the filed settings and do as we told src='{$sidebarAd->images->url} I don't know why src='{$sidebarAd->images->url}{$sidebarAd->images} works... have to investigate Link to comment Share on other sites More sharing options...
OllieMackJames Posted March 28, 2013 Author Share Posted March 28, 2013 src='{$sidebarAd->images->first->url} Thanks diogo, that works. I will add a new field for the ads with only one image allowed. And then will use: src='{$sidebarAd->images->url} Thanks again Manfred62 and diogo. Link to comment Share on other sites More sharing options...
diogo Posted March 28, 2013 Share Posted March 28, 2013 I don't know why src='{$sidebarAd->images->url}{$sidebarAd->images} works... have to investigate Ok, this makes sense because (and I didn't know this) when "images" is a multiple images field {$page->images->url} returns the url to the images folder for that page, and {$page->images} returns all the file names of the images in this format (image1.png|image2.png). If there is only one image in that field it will return simply (image1.png), so {$page->images->url}{$page->images}, will logically return (/site/assets/files/1001/image1.png). This won't work if there is more than one picture in the field, because the result would be a non existing url (/site/assets/files/1001/image1.png|image2.png). Link to comment Share on other sites More sharing options...
OllieMackJames Posted March 28, 2013 Author Share Posted March 28, 2013 Makes sense in Ok, this makes sense because (and I didn't know this) when "images" is a multiple images field {$page->images->url} returns the url to the images folder for that page, and {$page->images} returns all the file names of the images in this format (image1.png|image2.png). If there is only one image in that field it will return simply (image1.png), so {$page->images->url}{$page->images}, will logically return (/site/assets/files/1001/image1.png). This won't work if there is more than one picture in the field, because the result would be a non existing (/site/assets/files/1001/image1.png|image2.png). makes sense indeed, slowly getting my head around PW and how php works, never did much of that but this time around (after years of having all done for me) I decided to dive in a bit and seeing it is all logical, it starts to make more and more sense and I even enjoy it as well. With lots of help of you guys here, thanks! 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