Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to have a gallery in a post or a page (code in template or via shortcode). The images of this gallery should be pulled automatically from posts of one or more categories of my choice.

So as soon as a new post is saved all images (one or more) attached to this post should automatically be added to the gallery.

It'd be perfect if this gallery could be placed on a separate page. If a plugin does the trick: fine, too.

share|improve this question
    
What have you tried already? What code was used? –  Nicole Oct 30 '12 at 20:01
    
thanks for respomdimg. I havn't tried anything really, yet. I'm not a programmer, so I searched for a code snippet or a plugin which would do the trick. The only thing I found after days of searching is "complety gallery manager" (premium). Regards stephan –  user23100 Oct 30 '12 at 23:09
    
Stephan, please use the comments to clarify other users doubts, or edit the Question to add new information relevant to it. Reading the FAQ will enhance your further use of this site. –  brasofilo Oct 30 '12 at 23:36

1 Answer 1

In form of a shortcode to render a default WordPress [gallery] shortcode including all attachments of the given categories:

/**
 * Usage: [catgallery cat="4,5"]
 * Attribute: array of category IDs
 */

add_shortcode('catgallery', 'wpse_70989_cat_gallery_shortcode');

function wpse_70989_cat_gallery_shortcode($atts) 
{
    // $return = ''; // DEBUG: enable for debugging
    $arr = array();
    $cat_in = explode( ',', $atts['cat'] );
    $catposts = new WP_Query( array(
        'posts_per_page'    => -1
    ,   'category__in'      => $cat_in
    ) );

    foreach( $catposts->posts as $post)
    {

        // DEBUG: Enable the next line to print the post title
        // $return .= '<strong>' . $post->post_title . '</strong><br />';

        $args = array( 
            'post_type'     => 'attachment'
        ,   'numberposts'   => -1
        ,   'post_status'   => null
        ,   'post_parent'   => $post->ID 
        ); 
        $attachments = get_posts($args);

        if ($attachments) 
        {
            foreach ( $attachments as $attachment ) 
            {
                // DEBUG: Enable the next line to debug the attachement object
                // $return .= 'Attachment:<br /><pre>' . print_r($attachment, true) . '</pre><br />';
                $arr[] = $attachment->ID;
            }
        }

    }
    // DEBUG: Disable the next line if debugging 
    $return = do_shortcode( '[gallery include="' . implode( ',', $arr ) . '"]' );
    return $return;
}

Read the documentation to fine tune the code to your specs.

share|improve this answer
    
Thank you so much brasofilo an sorry for nut using the commetn function (twice...!). Got it now. ;) I insertet your code into my functions php file and wrote a post with [catgallery cat="1"]: nothing happens. Any hints for a non-programmer/beginner? Thanks again! Stephan –  Stephan Oct 31 '12 at 9:02
    
Sorry Stephan, there was a mistake in the code when extracting the categories IDs from the shortcode. It has to be converted to array as it comes as a string. :::: Anyway, replace it in full in your functions.php. And follow the DEBUG instructions in the code to check what's going on behind the scenes. –  brasofilo Oct 31 '12 at 12:28
    
thanks again! Unfortunately, nothing shows up, when I replace the code in my functions.php file. When I add a post with "[catgallery cat="1"]" (or cat="48" or whatever) the shortcode itself is displayed in the post. that's it. Also, removing the // for debuggung has no effekt, . –  Stephan Oct 31 '12 at 12:49
    
If the shortcode itself is being printed, the add_shortcode function is not being called... Are you sure is the right functions.php? :::: Or: create a new PHP file, put a basic plugin header information, paste the code, upload to your plugins folder and activate. –  brasofilo Oct 31 '12 at 12:58
    
Here we go: :) 1) SORRY: I made a small but vital mistake while pasting your code, I double checked and found my mistake: now images are showing up! 2) THAAAAANK YOU!!! 3) one (hopefully) last question: it seems only newly added posts with images and existing featured images are added. is it possible to add attached images of already existing posts? regards Stephan –  Stephan Oct 31 '12 at 13:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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