WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. mores
    Member
    Posted 6 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

    <?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";
    	}
    }
    ?>
  2. ambarino
    Member
    Posted 5 years ago #

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

  3. Edouard Duplessis
    Member
    Posted 5 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
    Member
    Posted 5 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
    Member
    Posted 5 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

    <h1>Headline</h1>
    <?php postimage('thumbnail'); ?>
    Content ...
  6. Michiel
    Member
    Posted 5 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
    Member
    Posted 5 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
    Member
    Posted 5 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.

    Thanks

  9. aledan
    Member
    Posted 5 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!

    Aledan

  10. lhoylhoy
    Member
    Posted 5 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