Load post attached images on a single page site with fancybox

I think I understand what you are trying to achieve now. You will need to modify the single.php (or single-post-type.php if it is a CPT) to run, via ajax, your gallery or whatever if the content is being viewed through ajax. Here is something (modified a bit) I have used in the past to do just that.

This is the main loop, pretty much just as you indicated above. We assume fancybox is loaded and ready.

while($loop->have_posts()) : $loop->the_post();
    echo '<a href="'.get_permalink().'" class="fancybox fancybox.ajax">'.
    get_the_post_thumbnail($post->ID, 'some-size', array('class' => 'some-class')).'</a>';
endwhile;

Add this function to functions.php. It will return true if the content is being viewed through an ajax request.

// content loading in ajax? 
function is_ajax() {
    return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && 
    $_SERVER['HTTP_X_REQUESTED_WITH']=="XMLHttpRequest");
}

Then on your single, you’ll want to figure out if you’re in ajax or not, to create something a bit like this:

if(is_ajax()){ 
    echo '<div class="gallery-holder">';
} else {
    get_header(); 
    // put any other divs that need to happen in a normal page here
}

// do something with your images here, such as an attachment loop

if(is_ajax()){ 
    echo '</div>'; // close anything we opened
} else {
    get_footer(); 
    // put any other closing divs here
}
wp_reset_query();

Realize that many people “tab browse” and so will open your thumbnail preview in a new tab, so this will allow your page to still be viewed in the alternative method.

It is also up to you how you want to run through all the images on the single.php.