Jump to content

Get first image and title of children pages


AnotherAndrew
 Share

Recommended Posts

I am trying to output a list of link to children pages that display the child's title and the first image on the child page. I am doing that ok but I do not know how to return a thumbnail size image rather than full size. My image field is called "selectedimages".

This is what I have:

<?php 
   if ($page->children) {
            foreach($page->children as $selectedthumbs) {    

                $firstimage = $selectedthumbs->selectedimages->first();

                echo "<a href='{$selectedthumbs->url}'><img src='{$firstimage->url}' alt='{$firstimage->description}'></a>";    
        }}
?>

Link to comment
Share on other sites

Owzim, I tried this but I am still getting full size images. I'm not using the thumbnail module.

<?php 
   if ($page->children) {
            foreach($page->children as $selectedthumbs) {    
                $firstimage = $selectedthumbs->selectedimages->first();
                $firstimage->size(100, 100);
                echo "<a href='{$selectedthumbs->url}'><img src='{$firstimage->url}' alt='{$firstimage->description}'></a>";    
        }}
    ?>

Link to comment
Share on other sites

You are not doing anything with the image, you are just applying the size method without assigning it to anything. The method actually returns that thumbnail, so use it like that:

<?php 
if ($page->children) {
    foreach($page->children as $selectedthumbs) {
        $firstimage = $selectedthumbs->selectedimages->first();
        $thumb = $firstimage->size(100, 100);
        echo "<a href='{$selectedthumbs->url}'><img src='{$thumb->url}' alt='{$firstimage->description}'></a>"; 
    }
}
?>
 
  • Like 2
Link to comment
Share on other sites

$selectedthumbs->title
 

is not the page's title but the title of the image and by default images of the images field type don't have a title, so you should use the description as well.

Link to comment
Share on other sites

Actually, I think $selectedthumbs->title IS the post's title...

<?php 
   if ($page->children) {
            foreach($page->children as $selectedthumbs) {

Of course $firstimage has no title because it's an image, but $selectedthumbs is the actual containing page so it has a title, my bad.

Link to comment
Share on other sites

To make this more real world code. It's best practice to better check if there really is children and images

if ($page->child->id) { // at least one child page viewable (access), access save
    foreach($page->children as $selectedthumbs) {
        if(!count($selectedthumbs->selectedimages) continue; // no image found continue foreach
        $firstimage = $selectedthumbs->selectedimages->first();
        $thumb = $firstimage->size(100, 100);
        echo "<a href='{$selectedthumbs->url}'><img src='{$thumb->url}' alt='{$firstimage->description}'></a>"; 
    }
}

You can also use the fast check

if($page->numChildren) {...

But it's not access aware and also includes unpublished pages. BUT it's faster than for example

if($page->children()->count()){ ..

If you have 1 million children (cite Ryan) it would matter and children->count() example would get slow.

So what if I numChildren() returns true but there's no page viewable by the visitor? It doesn't matter if you only output something inside the foreach(...), because there you use $page->children which does only return pages published and viewable pages.

But this could leave you with empty UL's:

if ($page->numChildren) {
    echo "<ul>"; // this may output even if the following foreach doesn't find children
    foreach($page->children("limit=25") as $child) {
        echo "<a>$child->title</li>";
    }
    echo "</ul>"; // this also
}

End of lesson. :D

  • Like 4
Link to comment
Share on other sites

But it's not access aware and also includes unpublished pages. BUT it's faster than for example

In 2.3, there are also these two additions (so far, undocumented). Either one returns the number of visible children: 

$page->numChildren(true); 
$page->numVisibleChildren;  

I will be adding these to the API pages, along with all the other 2.3 additions very soon. 

  • Like 7
Link to comment
Share on other sites

  • 4 months later...

Using this bit on a parent page's template, with the image field first_image_child_page on the children pages I can output this field image from the current parent page. 

  <!-- GET FIRST IMAGE FROM CHILDREN's ON CURRENT PARENT PAGE -->
      <?php
        echo "<div class='row-fluid'>";
        echo "<ul class='thumbnails'>";
        if ($page->child->id) { // at least one child page viewable (access), access save
        echo "Todas as marcas apresentadas são exclusivo direito do seu fabricante.";
        foreach($page->children as $selectedthumbs) {
        if(!count($selectedthumbs->first_image_child_page)) continue; // no image found continue foreach
        $firstimage = $selectedthumbs->first_image_child_page->first();
        $thumb = $firstimage->size(100, 0);
        echo "<li class='span2'> <a href='{$selectedthumbs->url}'><img class='thumbnails' src='{$thumb->url}' alt='{$firstimage->description}'></a></li>"; 
    }
}  
        echo "</ul>";
        echo "</div>";

 ?>

I tried $pages->get("/the-parent-page/"); but there is something wrong with my syntax, and probably there is better way to do this.

How can I output this children's pages image field, that are under this particular parent page throughout the other site pages? Thank you.


 

Link to comment
Share on other sites

In your code, everywhere you have "$page", replace it by "$chosenPage", and above that code do something like:

$chosenPage = $pages->get("/the-parent-page/");
  • Like 1
Link to comment
Share on other sites

Back to English. The rest of you, just ignore. We were talking about the FC Porto new football coach, and somehow @nfil added port wine to the conversation ;)

  • Like 2
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

×
×
  • Create New...