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.