Forums

How to pull image urls based on menu order in a post? (2 posts)

  1. HeroicNate
    Member
    Posted 1 month ago #

    I'm using :

    <?php
    global $wpdb;
    $attachment_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_parent = '$post->ID' AND post_status = 'inherit' AND post_type='attachment' ORDER BY menu_order DESC LIMIT 1");
    ?>
    
    <?php echo wp_get_attachment_url( $attachment_id ); ?>

    to pull out the first image attached to a post. Is there a way for me to pull out a specific image based on it's order besides just the first and last? Is there a way for me to pick the 2nd or 3rd image for instance?

  2. apljdi
    Member
    Posted 1 month ago #

    Use $wpdb->get_results and eliminate the LIMIT 1 and you'll get an object with all of the results in it. You can then pick first, second, third, whatever.

Reply

You must log in to post.

About this Topic