Jump to content

API not clear


froot
 Share

Recommended Posts

hello,

I have a template with two image-fields, one with label "logo" and the other one with label "coverimage".

When I put    

$entries = $page->children;
foreach ($entries as $entry) {
echo $entry->images()->url;
}

on the parent page, PW seems to output the path of the images.

How do I output the image from either field specifically? I find the API a little bit confusing…

$fields->find("selector")
$fields->get("name") //do I replace 'name' with the field's label?
$fields->get("selector") //or 'selector' with the field's label?
$field->name //does that output the field's name? where do I specify an image field's name, afaik it only has a label. Or do I replace "name"?
$field->label
$field->get($key) //what is $key?

And how do I go from here to output the url? What am I not getting?  

Is there a shortcode to just renders HTML with the image inside?

Link to comment
Share on other sites

19 minutes ago, fruid said:

echo $entry->images()->url;

Given that your field is named images, I am guessing it is a multi image field. This means the field can contain more than one image. This depends on how you set up the field (see its settings when editing the field.

Assuming this is a multi image field, you code above won't work.

19 minutes ago, fruid said:

How do I output the image from either field specifically? I find the API a little bit confusing…

Speaking in very general terms, there are two types of fields: Ones that return only a single value and ones that return multiple values. For instance, an email field will return only one value; the@email.tld. For the fields that can return multiple items, think about it this way...

Imagine you have a basket. The basket can contain several fruits. If someone asked you to "give me the fruit"? Your natural response (assuming you are willing to give;-)) would be "which one"?  This is because there are multiple fruits in the basket. By requesting the fruit, it implies they want not just any fruit but a particular fruit. That is the same thing you are asking in your code above; "Give me the URL" and ProcessWire is responding with which one? There are several images here, which one do you want? 

Back to the images, you will need to either specify which image's URL you want or have ProcessWire return the URLs of all the images in the collection (the field on that page). The latter means you have to loop through the images field (collection) as well.

Some code..

foreach ($entries as $entry) {
	$images = $entry->name_of_your_image_field;// e.g. $entry->logo
	// we want all images so we loop through the image field
	foreach($images as $image) {
      // just an example	
      echo $image->url;
	}
}

Edit

You access a field of a page using its (the field's) name. @see my edit above ($entry->logo).

If the image field is of type single, you don't need to loop through it. Just echo its content, e.g. echo $entry->logo;. In other words, there is no API variable called images in ProcessWire. 

Edited by kongondo
clarification
  • Like 5
Link to comment
Share on other sites

thanks for your clear explanations.

However, it doesn't work.

First of all, I only wanted a single image field (I now figured out how to set it to be a single field, namely under fields>details>formated value set to single item). I also now removed the overrides of that field within the template.

So I don't need to loop through the images alright, but I do have to loop through the "entry"-pages, the code being on the parent-page.

Here's my code

$entries = $page->children;

echo '<div><ul>';
foreach ($entries as $entry) {
    echo '<li><a href="' . $entry->website . '">'; //was $entry->domain
    echo '<img src="' . $entry->logo . '"/>';
    echo '<strong>' . $entry->title . '</strong></a></li>';
}
echo '</ul></div>';

The only thing that is put out is the title. Might the issue be the mixed quote marks?

thanks for your help!

EDIT: my bad, the <a> link works fine, I removed the override but forgot to change it in the code ? (see comment above)

 

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