Jump to content

$img->url just returns base path?


patzer
 Share

Recommended Posts

Hi. I've tried this a couple of different ways, but whenever I call $img->url like so:

<?php $img = $pages->get(1)->header_imgs->pop()->header_img ?>
      <img src="<?= $img->url ?>" alt="<?= $img->description ?>">
 

I get output like this:

<img src="/site/assets/files/1035/" alt="">
 

No file name. Any tips??

Link to comment
Share on other sites

Frankly I don't quite get this part of your code:

<?php $img = $pages->get(1)->header_imgs->pop()->header_img ?>

So is the image field name in you homepage template header_imgs or header_img?

If it's the first one and as the name suggest, it's defined to hold multiple images, you can get url like so:

<?php 
// get last image from array
$img = $pages->get(1)->header_imgs->last();

// this should also work
$img = $pages->get(1)->header_imgs->pop();

// get first image from array
$img = $pages->get(1)->header_imgs->first();

// output image url
echo $img->url;

For single image fields it works like this:

<?php 
// get image
$img = $pages->get(1)->header_img;

// output image url
echo $img->url;

PW API pages has good info about image field type: http://processwire.com/api/fieldtypes/images/

Link to comment
Share on other sites

Sorry, I should have specified that $header_imgs is a repeater. It has only one field at the moment, which is $header_img, an image type. I'm still unclear as to why I get the base path of the image rather than the full image URL.

From your example above, is 

<?php 
// get image
$img = $pages->get(1)->header_img;

// output image url
echo $img->url;

different from 

echo $pages->get(1)->header_img->url;

?

I've gotten around this before by appending $file->namebut this shouldn't be necessary?

Thanks!

Link to comment
Share on other sites

Those two output exactly the same - what better suits your need.

So if $header_imgs is a repeater I asume that $header_img field is defined for multiple images (like it is by default). Then you just have to remember that image field is also array and you can loop trough or get items using WireArray methods. So not much to add to your first attempt:

<?php 
// get first image from last repeater object
$img = $pages->get(1)->header_imgs->pop()->header_img->first(); 

// output relative url
echo $img->url;

// output absolute url
echo $img->httpUrl;
  • Like 1
Link to comment
Share on other sites

So if $header_imgs is a repeater I asume that $header_img field is defined for multiple images (like it is by default). Then you just have to remember that image field is also array and you can loop trough or get items using WireArray methods. 

Ahhhh! This is my confusion. Looks like I may not need a repeater field at all, as the image field is an array by default. (May keep it so I can add accompanying text, though. This is for a carousel.) Let me go experiment with the understanding that the image field is an array.

Edit: And, it worked! Man I was tearing my hair out about this.

Link to comment
Share on other sites

Cool!

Image field (or any other file field type) is array if it's allowed to hold more than one file. You can set this from field setup under Details tab: Maximum files allowed.
 

So your first attempt would also work if you set your header_img Maximum files allowed to 1.

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