WordPress.org

Support

Support » Plugins and Hacks » post image, image title, description … automatic gallery

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";
    	}
    }
    ?>

Viewing 9 replies - 1 through 9 (of 9 total)
  • ambarino

    @ambarino

    GREAT! Exactly what I was looking for! Many thanks and regards from Catalonia.

    nice … was looking for this…

    “spent day trying to figure out was where image titles, descriptions and captions were stored”

    me too

    Hi. This looks like what I need!! However… how do I use the function in my theme so I can control how images are displayed? Or should I just add it to functions.php?

    sorry for the late reply, but the “notification function” of this board is nonexistent.

    You need to copy my code and paste it into your functions.php.
    If the file functions.php does not exist in your theme directory, create it.

    Then you can use the function postimage() in your theme … like this

    <h1>Headline</h1>
    <?php postimage('thumbnail'); ?>
    Content ...

    Got it, this works.
    How could I avoid loading the WP Gallery and just use this function?
    At the moment I get my pictures twice:-)

    Remember, images are “assigned” to posts/Pages when you upload them, and the function reads those images.
    So, just upload the images and do not press the “insert in article” button.

    Hi there

    I’ve inserted galleries in my web-page but instead of having just one caption underneath the image I want about 6 lines of captions, each with a different value.

    Is this possible?

    Or is it possible to insert the Image description instead?

    And if so how can I do that? I’ve tried inserting your code into functions but it keeps crashing my site.

    Thanks

    I need display gallery, outside the loop, in a dedicated box only if images number is > 1.

    How I can do that with this function? Thanks a lot!

    Aledan

    hey sir mores, what if i don’t want an image in a post and i dont want that image space?

    I’m creating wp themes for a client I have no control on what he wants to do with his posts. How can I remove that empty space if there is no image?

    thanks I really need this..

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘post image, image title, description … automatic gallery’ is closed to new replies.
Skip to toolbar