Jump to content

Recommended Posts

Posted

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>";    
        }}
?>

Posted

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>";    
        }}
    ?>

Posted

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
Posted

Oh, ok. Now I see the difference! Thanks.

And if I want to display the page title I do this:

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

Posted
$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.

Posted

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

<?php 
   if ($page->children) {
            foreach($page->children as $selectedthumbs) {
  • Like 1
Posted

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.

Posted

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
Posted

@Soma, what would we do without your lessons?!  :P

I didn't know the difference and how to use them... now I have to recheck my code...

  • Like 1
Posted
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
  • 4 months later...
Posted

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.


 

Posted

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
Posted

Thanks Diogo! that was fast probably cause we are on the same timezone.

biba o FCP!(Sorry I just couldn't resist)

Posted

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

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
×
×
  • Create New...