• Hey all,

    I’m setting up a site for photoblogging, and I want a gallery page of all the images from my image posts, with the gallery automatically updating every time I add a new image post.
    I’m adding all my frontpage image posts to a category called “photos” and want all those images to show up on the one page.
    I’ve added a shortcode to my functions, but its not working.

    Here is my function.

    add_shortcode('catgallery', 'wpse_70989_cat_gallery_shortcode');
    
    function wpse_70989_cat_gallery_shortcode($atts)
    {
    
        $arr = array();
        $cat_in = explode( ',', $atts['cat'] ); //get category by ID
        $catposts = new WP_Query( array(
            'posts_per_page'    => -1
        ,   'category__in'      => $cat_in
        ) );
    
        foreach( $catposts->posts as $post) //get posts from category
        {
            $args = array(
                'post_type'     => 'attachment'
            ,   'numberposts'   => -1
            ,   'post_status'   => null
            ,   'post_parent'   => $post->ID
            );
            $attachments = get_posts($args); //should get attachment info from posts
            if ($attachments)  //array seems to be empty at this point
            {
                foreach ( $attachments as $attachment )
                {
    
                    $arr[] = $attachment->ID;
    
                }
            }
    
        }
        $return = do_shortcode( '[gallery include="' . implode( ',', $arr ) . '"]' );
       //outputs [gallery include=""] into site
        return $return;
    }

    Basically, this searches for the category ID, gets the posts in that category and should lookup attachments on those post to add to the [gallery include=""] shortcode.

    I’ve narrowed down the problem to the attachments,
    the function is getting all the posts from my ‘photos’ category, but it is not getting the attachment information from the posts.

    I’m not an expert on php coding, so if anyone can help me out and let me know where I went wrong, or a better way to do this, I’d greatly appreciate it.

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You should use the conventional WP style loop when iterating through the result of WP_Query instead of using foreach. The result is not an array, it is a query object with its own methods to run the loop. Like so:

    if ( $catposts->have_posts() ) {
        while ( $catposts->have_posts() ) {
    	$catposts->the_post();
            $args = array(
                'post_type'     => 'attachment'
            ,   'numberposts'   => -1
            ,   'post_status'   => null
            ,   'post_parent'   => get_the_ID()
            );
            $attachments = get_posts($args);
            if ($attachments)
            {
                foreach ( $attachments as $attachment )
                {
                    $arr[] = $attachment->ID;
                }
            }
        }
    }

    Note the method of getting the post_parent ID has changed as well. If you adopt this style, I believe your shortcode will work as expected.

    FWIW, if you used get_posts() instead of WP_Query to get the posts in the categories, your foreach approach would have worked fine. That function does return an array.

Viewing 1 replies (of 1 total)

The topic ‘Gallery by Category’ is closed to new replies.