mike62

[SOLVED] Image path incomplete

Recommended Posts

I am new to PW, and am trying to get an image to display, from a field in my template. Here's the code I'm using:

<?php 
	$image = $page->home_header_image;
	echo "<img src='{$image->url}' alt='{$image->description}' />";
?>

I'm following the tutorial on this here.

When I save the template and reload the page, the path to the image is incomplete. If just echoes:

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

It omits the name of the image, at the end of the path.

Here's the site I'm working on:

http://dev.delucaswpg.webfactional.com

A couple caveats:

  1. I'm using ProcessWire 2.3.0 (tried updating the site and everything went berserk because of some plugin dependencies; point being: I can't update to the latest PW).
  2. I tried using the ->size thing and it gave me an error, saying it couldn't use 'size' in this context. Which I also thought was weird.

Any ideas? Thanks!

Share this post


Link to post
Share on other sites

Regarding your no URL issue, most like you image field $image is set to hold multiple images (hence returns an array - whether it has only one image or or more). If that's the case, you will need to loop through it. 

Edited by kongondo
  • Like 1

Share this post


Link to post
Share on other sites

Hi @mike62,

thats much at once, updating from 2.3 to the latest version. Depending on the used third party modules in that site, you may be able to update to version 2.7.3, what was the last version before changing to 3.0.x, what included a change to use PHP namespace.

So, why exactly do you want to upgrade? Do you need some special module that isn't available for pw 2.3.x?

With that $image thing, please check if you are using a single image field or if it is a multiimage field. If it is a multi image field (that is by default), you need to to select one image out of the collection, maybe ->first(). Also you should check if there is at least one image stored within the field:

$image = $page->home_header_image->first();
if('' != $image->url) echo "<img src='{$image->url}' alt='{$image->description}' />";

 

  • Like 2

Share this post


Link to post
Share on other sites

Just wondering, since this is a dev site, is it a new site? If yes, was there a reason to install ProcessWire 2.3 or that's how the site was handed over to you?

Edit: Welcome to the forums and ProcessWire.. :).

Edited by kongondo
  • Like 2

Share this post


Link to post
Share on other sites

@kongondo The site was handed to me like this, and there's too much to rework to start from scratch, so I'm just doing a re-skin. 

@horst I don't need to update the site. Just mildly concerned about using outdated (and possibly not secure?) software.

The below code now works great, thanks for the tip!

<?php 
	$image = $page->home_header_image->first()->size(1280,420);
	echo "<img src='{$image->url}' alt='{$image->description}' />";
?>

I had no idea that the "Image" field was for more than one image. I don't see a way to make a "single image" image field... am I missing something? I don't really need it at this point, since the code change works. Just curious. Thanks again!

Share this post


Link to post
Share on other sites

No, PW 2.3 is no insecure software. It's only missing some tons of new features. :)

I'm also have some PW 2.3 versions running until now, (and don't see any reason to update them).

  • Like 2

Share this post


Link to post
Share on other sites
15 minutes ago, mike62 said:

I had no idea that the "Image" field was for more than one image. I don't see a way to make a "single image" image field... am I missing something?

For ProcessWire 2.3

pw23-image-field-settings.thumb.png.6014949f8d1d90c825c71aa48ab98fb9.png

  • Like 4

Share this post


Link to post
Share on other sites

Ignore this (is there a way to delete a post in the forum completely?)

Edited by mike62
Template file was getting cached and wasn't displaying correctly for me, making me think something wasn't working.

Share this post


Link to post
Share on other sites
54 minutes ago, mike62 said:

Ignore this (is there a way to delete a post in the forum completely?)

Mods can do it but let's leave this here to help the next guy.

Edited by kongondo

Share this post


Link to post
Share on other sites
On 10/25/2017 at 12:57 PM, mike62 said:

I'm using ProcessWire 2.3.0 (tried updating the site and everything went berserk because of some plugin dependencies; point being: I can't update to the latest PW).

If I was actively developing a site, I would take the time to fix any plugin dependencies - you are missing out on so much by being on such an old version. I have updated many site from 2.3/2.4 to 3.x with minimal problems.  Sometimes errors pop up that look bad, but only take a few minutes to fix.

Just my 2cents worth!

  • Like 1

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By gregory
      Hi guys.
      I would like to show only the first image as a preview of a slideshow. The code below shows the first image but is repeated. Do you know a solution? Thank you
      //Call image foreach($story->galleria as $img) { echo "<img src='{$story->galleria->first()->url}' alt='{$story->galleria->first()->description}'>"; }  
    • By anderson
      Hi all,
      I'm a new to website building. Learned some CRASH course of js,jquery,php. Then I found CMS. Still learning around forum, youtube....
      Anyway, please help me with some beginer questions:
      1, About template - please correct me if I understand wrong : every page should be (or recommended) built on a template. So if in total I'll have 10 pages, 2 of them have same layout, I'll need 9 templates.  And, what fields a page includes, is not defined in page, but defined in the template that page uses. 
      2,  Where to see what modules I've installed? is it in "Modules - Site"? 
      3, I installed "PageTable Extended", then what?  As in a youtube tutorial, it should appear in Setup tab, but it doesn't.  What's in there: templates,fields,logs,comments. (I installed "Uikit 3 site_blog profile".)
      4, I did a search in Processwire website for the famous "repeater matrix" module, and can not find it, there's a Repeater, as well as a Matrix. Is it not a module?
      5, I watched this youtube tuts: https://www.youtube.com/watch?v=IHqnLQy9R1A
      Anybody familiar with this tuts please help: 
      After he analysed a target webpage layout he wanted to mimic, he created some fields, some template, then based on those he created a page and input some "content" in there, then clicked "view", it's just some text. So, here comes my question, he copied a folder "assets" (subfolders are: css,fonts,js,img) over, then the page have the appearance/layout he wanted to mimic. Where does that assets folder come from?
      Appreciate any help.
    • By Robin S
      Another little admin helper module...
      Template Field Widths
      Adds a "Field widths" field to Edit Template that allows you to quickly set the widths of inputfields in the template.

      Why?
      When setting up a new template or trying out different field layouts I find it a bit slow and tedious to have to open each field individually in a modal just to set the width. This module speeds up the process.
      Installation
      Install the Template Field Widths module.
      Config options
      You can set the default presentation of the "Field widths" field to collapsed or open. You can choose Name or Label as the primary identifier shown for the field. The unchosen alternative will become the title attribute shown on hover. You can choose to show the original field width next to the template context field width.  
      https://github.com/Toutouwai/TemplateFieldWidths
      https://modules.processwire.com/modules/template-field-widths/
    • By louisstephens
      I have done a bit of searching, but I can not seem to find an actual answer. I have a list of services as child pages under "Services". I can output the services just fine, but I cant wrap my head around how to group them "alphabetically" like:
      Services A - Service "A" 1 - Service "A" 2 - Service "A" 3 B - Service "B" 1 - Service "B" 2 - Service "B" 3 C - Service "C" 1 - Service "C" 2 - Service "C" 3 Has anyone achieved this type of functionality before?
    • By Falk
      Hi!
      I have lots of blogposts in my pagetree and I want the pagelabel to include the value of the post’s datefield (only the year).
      Adding date | title in the template settings results in something like…
      1536012000 | my blogpost title
      Is it possible to convert the timestamp to a readable date?