Skip to content

Instantly share code, notes, and snippets.

@git-willie
Created November 29, 2016 20:12
Show Gist options
  • Save git-willie/ebd8ca168e936d6795d2475e140d4d65 to your computer and use it in GitHub Desktop.
Save git-willie/ebd8ca168e936d6795d2475e140d4d65 to your computer and use it in GitHub Desktop.
[Wordpress] Load content via Ajax
// *** Load gallery on division template via Ajax ***
function load_ajax_content () {
$pid = array( 'p' => $_POST['post_id'], 'post_type' => 'gallery' );
$the_query = new WP_Query( $pid );
if ($the_query->have_posts()) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$images = get_field('photos');
if( $images ):
$data .= '<div class="gallery-slider">';
foreach( $images as $image ):
$data .= '
<div style="background-image: url('.$image["sizes"]["fp-medium"].');">
<a target="_blank" class="button-pin" href="https://pinterest.com/pin/create/button/?url=&media='.$image["sizes"]["fp-medium"].'&description="><i class="fa fa-pinterest-p"></i>in</a>
</div>
';
endforeach;
$data .= '</div>';
endif;
$data .= '
<div class="row gallery-caption">
<div class="columns large-5">
<h3 class="entry-title">'.get_the_title().'</h3>
</div>
<div class="columns large-7">'.get_the_content().'</div>
</div>
';
}
}
else {
echo '<div id="postdata">'.__('Didnt find anything').'</div>';
}
wp_reset_postdata();
echo '<div id="postdata">'.$data.'</div>';
}
add_action ( 'wp_ajax_nopriv_load-content', 'load_ajax_content' );
add_action ( 'wp_ajax_load-content', 'load_ajax_content' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment