• Resolved austindixon

    (@austindixon)


    If I upload one picture with each new post, is there a way I can call the images from the five most recent posts, without having to go back each time to change the code? I’m using jondesign.net’s smooth slideshow, and I would like the images to update as I post new material, but php isn’t my thing (x/html is, lol). I’m just looking for something that basically asks:

    <php get (most recent image) ?>
    or
    <php get (second most recent image) ?>

    I know that’s not any kind of real php code, I just used that to show what I’m wanting it to do. Is this possible in WordPress?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Getting the latest 5 attachments can be done with a slightly modified Loop, I think.

    <?php
    $my_query = new WP_Query('post_type=attachment&showposts=5');
    while ($my_query->have_posts()) :
    $my_query->the_post();
    echo wp_get_attachment_url(get_the_ID());
    endwhile;
    ?>

    That gets and outputs the URL’s of the last 5 attachments. I think. Didn’t test it, just wrote it up real fast.

    Thread Starter austindixon

    (@austindixon)

    Thanks for the fast help.

    That would be very helpful if it works, but I get the general idea.

    Is there a way to call, say the second most recent image or the third most recent image individually?

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Is there a way to call, say the second most recent image or the third most recent image individually?

    Sure, possibly, but it’s an extra database hit. Better to get all the ones you need and then use them directly.

    Let’s say you need all 5 but at different places in the code. You’d want something like this:

    $my_query = new WP_Query('post_type=attachment&showposts=5');
    while ($my_query->have_posts()) :
    $my_query->the_post();
    $attachments[] = wp_get_attachment_url(get_the_ID());
    endwhile;
    ... later in your code ...
    // need second attachment
    echo $attachments[1]; // zero-based index
    // need third attachment
    echo $attachments[2]; // zero-based index

    …and so on. Get the ones you want right away, reference them later. Getting things one at a time is bad, because the query cost is higher than the retrieval cost.

    Thread Starter austindixon

    (@austindixon)

    That was exactly what I was looking for. I’ll be wary of it, like you said, but I should only need that in one instance on the entire site.

    Thanks again for the quick help and great info!

    Don’t know if I was doing something wrong, but I had to add &post_status=inherit to the parameter because the SQL generated without that didn’t give an OR option including this:

    $attachments = new WP_Query('post_type=attachment&post_status=inherit');

    I made it work with this….

    <?php query_posts('cat=3&showposts="1'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <a href='<?php the_permalink() ?>'><?php the_content(); ?></a>
    <?php endwhile; ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Can I use php to get the newest images?’ is closed to new replies.