Jump to content

Showing all images from a child page??


Jon E
 Share

Recommended Posts

Hi there,

If anyone could give me the code snippet to show all of the images from a child page that would be fantastic.

At the moment I can only show the first one?

I would love to show them all in list items...

Thanks,

Jon


  <div id="portfolio_list">
        
<?php
 
$features = $pages->find("template=project, sort=-date");
 
foreach($features as $feature) {
 
   $thumb = $feature->thumbnail ? $feature->thumbnail->width(800)->url : "http://i.imgur.com/1zxfxMW.jpg"; // if no thumbnail, replace by placeholder
 
   echo "<ul class='bxslider'>" .
"<li><img src='{$thumb}' /></li>
  <li><img src='{$thumb}' /></li>
  <li><img src='{$thumb}' /></li>
  <li><img src='{$thumb}' /></li>
</ul>" . //changed from $thumb->url to only $thumb
 
   "</ul>";
 
}
 
?>
 
</div>
Link to comment
Share on other sites

It's a little hard to follow exactly how you have your site structure, but you should be able to find the answer for your problem here:

http://processwire.com/api/fieldtypes/images/

The very first example shows how to iterate through all images on a page:

foreach($page->images as $image) {
  echo "<img src='$image->url'>"; 
} 

If you need to get the images from a child page then simply get the child page first using a selector:

$childpage = $page->child(selector to get specific child);

Then foreach through $childpage->images

  • Like 1
Link to comment
Share on other sites

The problem here is that you are outputting the same exact thumbnail 4 times. I'm guessing maybe something like this is what you wanted? 

<div id="portfolio_list">
<ul class='bxslider'>
<?php

$features = $pages->find("template=project, sort=-date");
foreach($features as $feature) {
  $thumb = $feature->thumbnail ? $feature->thumbnail->width(800)->url : "http://i.imgur.com/1zxfxMW.jpg";
  echo "<li><img src='$thumb' /></li>";
}
?>
</ul><div>

Either that, or if you were wanting to output 4 images from each $feature page, then you'd want another foreach within that loop that iterates over the images in $feature.

Link to comment
Share on other sites

  • 2 weeks later...

Hi,

Would someone be able to give me the code so that I can see how this can be achieved - basically I want to output all the images from pages with certain template types on a parent page.

My whole code is:

<?php include 'head.inc'; ?>

<div id="portfolio_list">
        
<?php

$features = $pages->find("template=project, sort=-date");

foreach($features as $feature) {

   $thumb = $feature->thumbnail ? $feature->thumbnail->width(800)->url : "http://i.imgur.com/1zxfxMW.jpg"; // if no thumbnail, replace by placeholder

   echo "<ul class='bxslider'>" .
"<li><img src='{$thumb}' /></li>
  <li><img src='{$thumb}' /></li>
  <li><img src='{$thumb}' /></li>
  <li><img src='{$thumb}' /></li>
</ul>" . //changed from $thumb->url to only $thumb

   "</ul>";

}

?>

</div>

<?php include 'foot.inc'; ?>

So rather than outputting the thumb each time I want to cycle through all the images in that post...

If someone could show me exactly how that would be done that would be great as I'm so stuck......

Link to comment
Share on other sites

I'm not sure I understand the problem, especially when the solution is in this thread above. Also not knowing what images field you have and if you mean the "thumbnail", is that a multiple image field or a single image? Guessing you mean by images the field "$page->images" 

To output all images from the page with the image field being "images".

foreach($page->images as $image) {
  echo "<img src='$image->url'>"; 
}

If I'm right in your code this would go like:

if($feature->images->count()) {
    echo "<ul class='bxslider'>";
    foreach($feature->images as $image){
        echo "<li><img src='$image->url'/></li>";
    }
    echo "</ul>";
}
Link to comment
Share on other sites

Hello,

Yes I have images in an images field for all of the child pages...

I want to achieve that - is it something like below?

<?php

$features = $pages->find("template=project, sort=-date");

foreach($page->images as $image) {
  echo "<img src='$image->url'>"; 
}

 if($feature->images->count()) {
    echo "<ul class='bxslider'>";
    foreach($feature->images as $image){
        echo "<li><img src='$image->url'/></li>";
    }
    echo "</ul>";
}

?>
Link to comment
Share on other sites

No now you're mixing things up... You're not foreach "looping" the features anymore.

I'm still not sure what parent and what childs you're saying. Maybe you can illustrate more precisely how the structure is and where the "images" and "thumbnail" really are. And where this code would be. 

My second code was meant to replace the part in your code :

echo "<ul class='bxslider'>" .
"<li><img src='{$thumb}' /></li>
<li><img src='{$thumb}' /></li>
<li><img src='{$thumb}' /></li>
<li><img src='{$thumb}' /></li>
</ul>"
. //changed from $thumb->url to only $thumb

"</ul>";

Which is confusing for me what this should mean.

And the first example is copied from adrians post above, which is just an pseudo code example how to loop all images on the page and output them.

I'm not sure how much you understand what you're doing so I'm sorry if this is all too much to understand. But the problem starts with a the kinda raw explanation of what you need or want to do and how the structure of your setup is.

PW allows for too much variations on how to setup things it makes it hard to "guess" sometimes also not knowing how much the guy posting really understands the concepts of PW and or PHP.

Link to comment
Share on other sites

Sorry, would you mind showing me how that code would look?

So rather than having that thumbnail 4 times I want to show all of the images uploaded to the image field within the page......

Thanks so much

Meaning what Ryan is speaking about to iterate (foreach) all images on that page and you asking how that code would looks like I showed you the code with:

if($feature->images->count()) {
    echo "<ul class='bxslider'>";
    foreach($feature->images as $image){
        echo "<li><img src='$image->url'/></li>";
    }
    echo "</ul>";
}

Which loops all images from the $feature page.

  • Like 1
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...