Odd behaviour with image URLs in subpages

Hey,

I’m trying to add an image to a subpage, and getting some odd URLs being returned. Here’s my file structure (shown as the content folder sorts it):

- About us
    - The team
        - Person 1
            person1.jpg
            person.txt

In my ‘person’ blueprint, I’ve got an ‘image’ input for selecting a file to use as the profile image. This all seems to work fine. However, when I try to use the image in the template as follows, it doesn’t work:

<?php foreach($page->children() as $subpage): ?>
    <div class="col-sm-4 col-md-3">
        <?php if($image = $subpage->profile_image()->to_file()): ?>
            <img src="<?php echo $image->url(); ?>" class="img-responsive">
        <?php endif; ?>

        <h3><?php echo $subpage->title(); ?></h3>
        <h4 class="light-header h5"><?php echo $subpage->jobtitle(); ?></h4>
    </div>
<?php endforeach; ?>

The title and jobtitle fields are fetched fine, but the image returns a url without the full structure (e.g. instead of xyz.com/about-us/the-team/person1.jpg it returns xyz.com/person1.jpg.

Any thoughts on what’s going on here?

Hey,

the function is called toFile() not to_file()

Thanks, that sorted it! Not sure if that was an old way of using the function (the site I’m upgrading is a year or so old now), but I could have sworn I’d seen ‘to_file’ a number of times on the internet when trying to find out what was going on!

Thanks again :slight_smile:

Fine :slightly_smiling_face: No, the function hasn’t changed ever since it was introduced in May 2015.

1 Like