Galleries of images attached to posts

If you plan on using WP’s built-in image custom fields, include this query on your single-project.php (assuming galleries will only be called on single project pages):

// this will get all images attached to that specific post
$imagequery = new WP_Query(
    array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image/jpeg',
    'post_parent' => $post->ID
    ));

if ($imagequery->have_posts()) {
    // loop to present those images here
}
else { // if no images attached to that post
    // fetch all posts from the taxonomy
    $catquery = get_posts(
        array(
        'post_type' => 'projects',
        'numberposts' => -1,
        'yourtaxonomyname' => 'yourtermname' // specify the 'category' here
        ));

    $postids = array();

    if ($catquery) {
        foreach ( $catquery as $onepost ) {
        array_push($postids, $onepost->ID); // make array of post ids
}

    $newimagequery = new WP_Query( // then query these ids to get their images
    array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image/jpeg',
    'post__in' => $postids
    ));

if ($newimagequery->have_posts()) {
    // loop to present images from other posts here
}


}