Fetching gallery images from a wordpress post

Sometimes, the default wordpress gallery plugin does not suit our requirements and we need some additional customization. You may use the gallery images embedded in a wordpress post (and create your own gallery or slideshow). To do that, you may find the following code useful:

<?php
$args = array(
'numberposts' => -1,         // -1 loads all posts. You may use specific numbers, if required/known.
'orderby' => 'menu_order',   // Ensures the images are in the order, as set in the media manager
'order'=> 'ASC',             // Ascending and descending
'post_mime_type' => 'image', // Only get images
'post_parent' => $post->ID,  // Important part. Without this the images can’t be loaded
'post_status' => null,
'post_type' => 'attachment'
);

$images = get_children( $args );
?>

And now, you may use the images with the following code:

<?php if($images){     foreach($images as $image){ ?>

        <img src="<?php echo $image->guid; ?>" alt="<?php echo $image->post_title; ?>" title="<?php echo $image->post_title; ?>" />

    <?php } ?> <?php } ?>

The code above may not be exactly what you are looking for, but it will give you some idea.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s