XML

Download ‘Get Post Image’ v1.0

Get Post Image is a plugin for Wordpress 2.0 and higher that allows you to retrieve images contained in posts and display them in a custom manner.

An example of where this can be useful is the “Distractions” section of my homepage. For this section images are retrieved from the first six posts in the distraction category, and then displayed as a list of hyperlinks to the actual article

Here are four examples of how to use Get Post Image in your theme. (Note: Get Post Image must be used within the post loop after you have retrieved the desired posts)

Example 1: Displaying the first post in an image

gpi_show_image(0, "http://www.myserver.com/default.jpg");

Example 2: Displaying all posts in an image using a CSS class of ‘post-img’

$image_count = gpi_get_image_count();
for ($i = 0; $i < $image_count; $i++)
{
gpi_show_image($i, "http://www.myserver.com/default.jpg", "post-img");
}

Example 3: Getting an image from a post and displaying it manually

$image = gpi_get_image(0, "http://www.myserver.com/default.jpg", false);
echo ‘<img src="’ . $image[‘url’] . ‘" ‘ . $image[’size’] . ‘ title="’ . $image[‘title’] . " />’;

Example 4: Getting an image from a post and displaying it with custom sizes

$image = gpi_get_image(0, "http://www.myserver.com/default.jpg", false);
echo ‘<img src="’ . $image[‘url’] . ‘" width="60" height="60" title="’ . $image[‘title’] . " />’;

This last code snippet shows how to retrieve six posts from a specified category and them display them as a list of image links.

<ul>
<?php
query_posts(‘category_name=distractions&amp;showposts=6′);
while(have_posts())
{
the_post();
$image_tag = gpi_get_image(0,"http://www.myserver.com/default.jpg");
$url = get_permalink();
echo "<li><a href=’$url’>$image_tag</a></li>";
}
?>
</ul>

(Note: If you were to do this for posts containing images of various sizes you may want to specify false in gpi_get_image to obtain the image info, then form the image tag manually specifying a desired width/height).

Update: Comments are now closed. Please use the forums for discussion of Get Post Image.


10 Responses to “Get Post Image for Wordpress”

Comments:

  1. Love this plug in. I just installed it but I’m having a minor issue with it. It displays the thumbs along with a link to the post. The problem is, below all the thumbs it displays each post in full.

    Check it out here.. http://www.dailypoa.com/?page_id=422

    This is the code I’m using.

    query_posts(’category_name=Our Galleries&showposts=12′);
    while(have_posts())
    {
    the_post();
    $image_tag = gpi_get_image(0,”http://www.myserver.com/default.jpg”);
    $url = get_permalink();
    echo “$image_tag“;
    }

    Any idea why the full post displays below it?

    Thanks!

  2. The plugin code is all fine. Somewhere else in the PHP file there’s another piece of code (probably also with a ‘while(have_posts())’ that is displaying the actual posts.

    Nice site btw ;)

  3. Thanks!!!!

    Maybe I’m just putting it in the wrong place…

    I’m creating a new page and I put the code in the page. I use phpexec to execute php commands within the page. I want to display my most recent galleries in the center of the page and I figured that is the easiest way to go about doing it. Is there another way to go about it?

    My ultimate goal is to have users click on a link that says “My Galleries” this will have 20 or so thumbnails and links to the post.

    I’m so lost when it comes to this stuff!

  4. I should note that the plugin works great when it’s in my sidebar. Just not in the body of a page.

  5. Thanks a lot Andrew for the beautiful job you have done with this plugin.
    I have a little question. Is there anyway I can use the example:3 above to manually pull out from the database a thumbnail image, while the actual post will display the full size image. I want to show the thumbnail with the excerpt of a post on the sidebar and show the full image on the post article.

    I am sure this is doable but if it does I’ll appreciated a lot.
    Thanks again

  6. On the last paragraph I meant to say that I am not sure what I was telling you about the plugin is doable and I would appreciate it if it does. I was a little tired I guess.

  7. Hello. I used the code that you use as an example to see six articles as a list of images, however, next to the images I still printed the title of the article.
    Some suggestion?

References on the Web (trackbacks and pingbacks):