post image, image title, description ... automatic gallery (10 posts)

  1. mores
    Posted 7 years ago #

    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

    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 ) {
    			$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";
  2. ambarino
    Posted 7 years ago #

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

  3. Edouard Duplessis
    Posted 7 years ago #

    nice ... was looking for this...

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

    me too

  4. Michiel
    Posted 7 years ago #

    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?

  5. mores
    Posted 7 years ago #

    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

    <?php postimage('thumbnail'); ?>
    Content ...
  6. Michiel
    Posted 7 years ago #

    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:-)

  7. mores
    Posted 7 years ago #

    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.

  8. mcdermos
    Posted 6 years ago #

    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.


  9. aledan
    Posted 6 years ago #

    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!


  10. lhoylhoy
    Posted 6 years ago #

    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..

Topic Closed

This topic has been closed to new replies.

About this Topic