Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm looking for a way in wordpress to iterate over an image gallery from within my template and ouput the images from it.

Note: I don't want images associated with a post. As I'm doing it from my page template, I can't rely on the current "post" to have it.

It looks like wordpress doesn't even have a gallery concept to begin with, so I guess what I might be looking for is a gallery management plugin with an API that I can fiddle with from my template...

share|improve this question
1  
Wordpress does have a gallery "concept" and it works pretty good. codex.wordpress.org/Gallery_Shortcode – Brian Fegter Oct 16 '11 at 3:33
You didn't read or don't understand my question fully. – Omega Oct 16 '11 at 4:09
You need to clarify your question. I did read through it and Wordpress does have a gallery concept as I shared. Your question is vague. Please provide code to back up what you're trying to do. – Brian Fegter Oct 18 '11 at 8:41
It's pretty clear, you may want to re-read it a few more times. – Omega Oct 29 '11 at 14:40
I agree, this needs clarification. Please add details where you want images to come from and which output you want to achieve. Since "gallery" in WP refers to very specific functionality, usage of this term for other things without details is confusing. – Rarst Oct 29 '11 at 15:11
show 1 more comment

closed as not a real question by toscho Jul 18 '12 at 21:25

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, see the FAQ.

1 Answer

Actually, WordPress does have a nice gallery shortcode, which will let you specify a post ID from which it will grab attachments.

Implementing that in your template is just a matter of...

<?php echo do_shortcode('[gallery id="some_number"]'); ?>

There are a whole host of other options, so you should check that out.

If you like to write code others have already written, you can roll your own gallery function.

<?php
function wpse31222_gallery( $post_id )
{
    $attachments = get_posts(
            array(
                'post_type'         => 'attachment',
                'post_mime_type'    => 'image',
                'numberposts'       => -1,
                'post_parent'       => $post_id
            )
        );
    foreach( $attachments as $a )
    {
        /**
        * do stuff here
        * functions like wp_get_attachment_image_src will come in handy
        */
    }
}

Then use it somewhere in your template...

<?php wpse31222_gallery( $some_post_id ); ?>

From your question, it seems like you're looking for some sort of system that you could use to give your users the option of managing a gallery's.

So, what I'd suggest is you register a post type, and only give it limited functionality (a title). You could then give users either a custom meta box or provide some way for them to easily get a shortcode to paste into any page or post. I did this recently on a plugin by adding a little "action link" to the posts screen (the list table of posts) and using smoke.js to overlay an alert with the shortcode for the user to copy.

Grabbing images associated with posts is the easiest way. You're not limited to accessing the current post, fortunately.

share|improve this answer
Complete overkill. There's got to be a simpler way. I want an image gallery, that's output as a ul. Can wordpress seriously not do this?? – Omega Oct 16 '11 at 4:07
You should edit your question to better explain what you want then. Two people have come to the same conclusion for solving your problem. – chrisguitarguy Oct 16 '11 at 17:22
Iterate through an image gallery, and output the images from it. So, an unordered list with images. Although WP seems to be sorely lacking in gallery functionality by default. Too many unmaintained or obsolete plugins to know which is right. – Omega Oct 29 '11 at 14:42
You need to at least try before you knock it. It's obvious you haven't even taken any of this advice. – Brian Fegter Oct 30 '11 at 13:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.