Jump to content

Image description


Kae
 Share

Recommended Posts

Hey, guys. I'm having some difficulty in outputting an image description as alt attribute. I created a field called featured_image. Using foreach I list all articles that use a certain template. I can display the image normally using:

$article->featured_image

$article->featured_image->url, works just fine.

But when i use

$article->featured_image->description

nothing happens =(

I'm just learning php and have no idea what am I doing wrong. Any help? Thank you.

Link to comment
Share on other sites

Sure! Here is the code  of the page:

<?php $articles = $pages->find("template=post-page, sort=-created"); ?>
                <?php
                    foreach ($articles as $article) { ?>
            <div class='article'>
                <a href="<?php echo $article->url; ?>"><img src='<?php echo $article->featured_image->url; ?><?php echo $article->featured_image ?>' alt='<?php echo $article->featured_image->description; ?>' /></a>
            </div>
               <?php } ?>
Link to comment
Share on other sites

That code is a little weird - I don't really understand why the image is working at all. What is the Details > Max Files Allowed setting for the featured_image field? If it is set to anything but 1, then this should work for you:

<img src='<?php echo $article->featured_image->first->url; ?>' alt='<?php echo $article->featured_image->first->description; ?>' />

If it's set to 1, then you won't need the "first".

Are you using the latest dev or 2.4 release? I am wondering if the new Formatted Value setting is at work here?

Link to comment
Share on other sites

Thank you, Adrian for your answer. I don't know why images work at all either!!! :rolleyes: I know the code could be better but I am just experimenting things. When I use <?php echo $article->featured_image->url; ?> I just get the path to the image, the image name was not there, so it didn't work. That is why I used <?php echo $article->featured_image ?> right beside it... and it worked. Am I doing something wrong here?

---

Putting 'first'  did solve problem. Thank you for that, but the code you suggested did not work for me. As I said, in the output I got the path to the image, but not its name.

Thank you for quick answer. ^-^

Edit: I was really hoping that <?php echo $article->featured_image->url; ?> would return me that image url including its name. But id doesn't , is this normal?

Link to comment
Share on other sites

Er - isn't it normal in html to have the <img> tag to display an image??

<?php echo "<img src='{$article->featured_image->url}' atl='{$article->featured_image->description}' >"; ?>

:)

Link to comment
Share on other sites

Putting 'first'  did solve problem. Thank you for that, but the code you suggested did not work for me. As I said, in the output I got the path to the image, but not its name.

 

Thank you for quick answer.  ^-^

 

Edit: I was really hoping that <?php echo $article->featured_image->url; ?> would return me that image url including its name. But id doesn't , is this normal?

Sorry I am confused here - you say that first solved the problem, but that my code didn't work?

By "name", do you mean the filename? You can access that using:

echo $article->featured_image->first->name;

That will give you just the filename. If you change it to:

echo $article->featured_image->first->filename;

you'll get the full disk path to the image, although this is probably not what you are looking for - useful for image manipulation, but not display. 

Basically you can access anything you want about the image. Take a look here:

http://cheatsheet.processwire.com/#files

Link to comment
Share on other sites

Sorry if I have confused you guys, I 'm not a native English speaker.

I'll try to explain what is happening.

Let's take a look at Joss code:

<?php echo "<img src='{$article->featured_image->url}' atl='{$article->featured_image->description}' >"; ?>

That code doesn't work for me. Images do not display. If I take a look at them, I see the path to the folder containing the image, but the filename is not in the output and so the image is not displayed  (though the description works).

I know I could have done what you guys suggested, but I didn't because the image was not showing.

In another words: why $article->featured_image->url  is only giving to me something like:

/post/July/postname/

??? (filename misssing!!!)

---

Adrian, when I said that the problem was solved... I meant the image description now works using your code ( that was the issue I created the topic for).

Link to comment
Share on other sites

I can't understand how $article->featured_image->url could possibly give /post/July/postname. Image paths in processwire look like:

/site/assets/files/1001/

Maybe I am just being slow this morning and someone else will see what is going on :)

Link to comment
Share on other sites

If you set your image field to allow a maximum of 1 image, then you can treat it as a single value.

However, if you have it set for more than one image, it is automatically an array, even if there is only 1 image in it.

If it is an array, you then have to tell it which image in the array you actually want - for instance using first - just like any other array in PHP.

However, normally you would allow more than 1 because you want to loop through them, for a gallery, for instance.

If you only want 1 image, I suggest you edit the field and set the max number of images to 1, just to save confusion.

  • Like 2
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

×
×
  • Create New...