Images
Images within the framework are handled by the Rila\Image
class. This class encapsulates the default WordPress image functionality, including image sizes, but lets you just use it without caring about all of the available functions like wp_get_attachment_image
, wp_get_attachment_image_src
, get_post_thumbnail_id
and etc.
Many properties in the framework will already contain an image. An example for this is the post featured image that is accessible through post.image
.
To output the image, simply echo it:
{{ post.image }}
If you need to create an image by yourself, you can use the rila_image( $id )
function and simply provide the ID of the image:
$attachment_id = 138;
$image = rila_image( $attachment_id );
echo $image->thumbnail;
You can access all available image sizes as properties of an image. If we want to use the thumbnail
size of the image above, we just need to add it to the end.
{{ post.image.thumbnail }}
What this property does is simply to switch the internal size, but still returns the image object. The actual conversion to an image tag is happening in the moment, when the image is converted to a string.
When you need only the URL/source of an image, you can use the src
attribute. You can also combine the attribute with an image size, when needed:
<!-- Full size -->
<div class="featured" style="background-image:url({{ post.image.src }})">
<!-- Intermediate size -->
<div class="featured" style="background-image:url({{ post.image.medium.src }})">
You can use Rila\Collection\Images
to retrieve multiple images from the database simultaneously and work with them:
$images = new Rila\Collection\Images( array( 125, 300, 221 ) );
However, in the better cases, this collection would be already mapped in your models. To do this, use the images
keyword:
$this->map(array(
'gallery_field' => 'images'
));
Next article: Dates
- Overview
- The basics
- Data types
- Extending the framework
- Helpers
- Others/Advanced
- Performance