post image, image title, description … automatic gallery
-
I needed this, and finally figured it out, so I thought I’d share.
What I spent days trying to figure out was where image titles, descriptions and captions were stored, and how they were to be pulled from the database.
And it’s actually simple. image title is post_title, image caption is post_excerpt, and image description is post_content.Now, by using special keywords, I can control the way attachment images are displayed. I wanted an automatic image gallery without the need to configure anything for each post. Some images were supposed to be in the header area, some are not supposed to be displayed in a gallery and the rest should just be listed beside a post.
So I’m now using this function in the functions.php
<?php function postimage($size=medium,$num=1,$lighbox=1) { if ( $images = get_children(array( 'post_parent' => get_the_ID(), 'post_type' => 'attachment', 'numberposts' => $num, 'order' => 'ASC', 'orderby' => 'ID', 'post_mime_type' => 'image',))) { foreach( $images as $image ) { $attachmenturl=wp_get_attachment_url($image->ID); $attachmentimage=wp_get_attachment_image($image->ID, $size ); $img_title = $image->post_title; $img_desc = $image->post_excerpt; if ($size != "full"){ echo '<a href="'.$attachmenturl.'" rel="lightbox" title="'.$img_desc.'">'.$attachmentimage.'</a>'.$img_title.''; } else { echo '<img src="'.$attachmenturl.'">'; } } } else { echo "No Image"; } } ?>
- The topic ‘post image, image title, description … automatic gallery’ is closed to new replies.