How to get the accompanying image title and caption for a random image?

I am using the random image function to display random images on the index.php of my zen photo gallery. I would like to also display the image caption.

I would like to place it:
(1) as the image description in the image tag.
(2) as a separate caption elsewhere on the page.

Is this possible, and if so how? Can anyone give me the code snippet? Below is the code I have on my index.php page

Any help greatly appreciated! Thanks.

S

`

<?php $randomImage = getRandomImages(); echo '<a href="'.htmlspecialchars(getURL($randomImage)).'" title="'.gettext('put image description here').'">imagegetSizedImage(getOption('image_size')) ).'" alt=."'.htmlspecialchars($randomImage->getTitle(), ENT_QUOTES).'" />';?>

`

Comments

  • Do it in just the same way as the code you posted gets the title.
  • thanks.

    Getting the title doesn't seem to be a problem so thats all good - but I am having a problem getting the description. I have been attempting to use `getImageDesc()` but it throws an error.

    The following code snippet returns the title fine,

    `

    '.htmlspecialchars($randomImage->getTitle(), ENT_QUOTES).'

    `

    But, using getImageDesc() throws an error.
    `

    '.htmlspecialchars($randomImage->getImageDesc(), ENT_QUOTES).'

    `

    I am obviously doing something wrong, any ideas?

    Sorry my php skills leave a little to be desired, but your help is genuinely appreciated.

    S
  • You will probably have to improve your PHP skills for a project like this. For instance, I imagine the error thrown said that the method `getImageDesc()` does not exist since its name is `getDesc()`. Review also the functions guides.
  • So, for the benefit of others who may want to achieve the same goal as myself goal:

    To print the caption or title of an image placed using the random image function use

    `getTitle()` and `getDesc()` an example of how to use this is:

    `<?php $randomImage = getRandomImages(); echo '<img src="'.htmlspecialchars($randomImage->getSizedImage(getOption('image_size')) ).'" alt=."'.htmlspecialchars($randomImage->getTitle(), ENT_QUOTES).'" />';?>`

    and

    `
    '.htmlspecialchars($randomImage->getTitle(), ENT_QUOTES).'
    `
    which outputs the title

    `
    '.htmlspecialchars($randomImage->getDesc(), ENT_QUOTES).'
    `
    which outputs the description

    The latter examples place the text within `` tags which is what i wanted, but you can edit that if you want.

    On a point of info: someone might want to update the User Guide: `getDesc()` does not exist since its name is listed as `getImageDesc()`
  • acrylian Administrator, Developer
    On a point of info: someone might want to update the User Guide: getDesc() does not exist since its name is listed as getImageDesc()
    You are wrong actually (beside the doc is generated automatically from the files directly). `getImageDesc()` is a template function depending on the image context: http://www.zenphoto.org/documentation/functions/_template-functions.php.html#functiongetImageDesc

    `getDesc()` is a direct image class method and needs to be used on the image object.
    http://www.zenphoto.org/documentation/classes/_Image.html#methodgetDesc
  • I stand corrected - I did look, but I didn't find it.
Sign In or Register to comment.