Created
September 6, 2019 14:22
-
-
Save ihorduchenko/9359c2acb05cafe432301d6d86fbcddc to your computer and use it in GitHub Desktop.
Load certain post clicking on link using Ajax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Ajaxify loading resources | |
function ajax_load_cases(){ | |
$al_id = $_POST['id']; | |
$al_type = $_POST['type']; | |
$al_args = array( | |
'p' => $al_id, | |
'post_type' => $al_type, | |
'posts_per_page' => -1 | |
); | |
$al_query = new WP_Query( $al_args ); ?> | |
<?php if ( $al_query->have_posts() ) : ?> | |
<?php while ( $al_query->have_posts() ) : $al_query->the_post(); ?> | |
<?php $post_id = get_the_ID(); ?> | |
<?php $thumb = get_the_post_thumbnail_url( $post_id, 'full'); ?> | |
<div class="card"> | |
<img src="<?php echo $thumb; ?>" class="card-img-top" alt="<?php the_title(); ?>"> | |
<div class="card-body"> | |
<h2 class="card-title"><?php the_title(); ?></h2> | |
<div class="card-text"><?php the_content(); ?></div> | |
<a href="<?php the_permalink(); ?>">View</a> | |
</div> | |
</div> | |
<?php endwhile; ?> | |
<?php else: ?> | |
<div class="text-block cl-muted py-3">No such post</div> | |
<?php endif; ?> | |
<?php die(); | |
} | |
add_action('wp_ajax_ajaxLoadCases', 'ajax_load_cases'); | |
add_action('wp_ajax_nopriv_ajaxLoadCases', 'ajax_load_cases'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($) { | |
var adminAjaxUrlInput = $('#adminAjaxUrlInput'); | |
var adminAjaxUrl = adminAjaxUrlInput.val(); | |
var ajaxAction = adminAjaxUrlInput.data('action'); | |
var ajaxLoad = $('.ajaxLoad'); | |
var responseEl = $('#ajaxResponse'); | |
function ajaxLoadPosts(url, action, id, type) { | |
$.ajax({ | |
url: url, | |
data: { | |
action: action, | |
id: id, | |
type: type | |
}, | |
type: 'POST', | |
beforeSend: function() { | |
responseEl.html('Loading...'); | |
}, | |
success: function(data) { | |
responseEl.html(data); | |
responseEl.removeClass('d-none'); | |
} | |
}); | |
} | |
ajaxLoad.on('click', function() { | |
var id = $(this).data('id'); | |
var type = $(this).data('post-type'); | |
ajaxLoadPosts(adminAjaxUrl, ajaxAction, id, type); | |
}); | |
})(jQuery); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Template Name: Ajax Cases */ | |
get_header(); ?> | |
<input id="adminAjaxUrlInput" type="hidden" value="<?php echo site_url() ?>/wp-admin/admin-ajax.php" data-action="ajaxLoadCases"> | |
<div class="container py-5"> | |
<div class="row"> | |
<div class="col-md-8"> | |
<?php $custom_query_args = array( | |
'post_type' => 'portfolio-item', | |
'posts_per_page' => -1, | |
'order' => 'DESC', | |
'orderby' => 'date' | |
); ?> | |
<?php $custom_query = new WP_Query( $custom_query_args ); ?> | |
<?php if ( $custom_query->have_posts() ) : ?> | |
<div class="row"> | |
<?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?> | |
<?php $id = get_the_ID(); ?> | |
<?php $post_type = get_post_type( $id ); ?> | |
<div class="py-1 col-6"> | |
<a | |
class="ajaxLoad" | |
href="javascript:" | |
data-id="<?php echo $id; ?>" | |
data-post-type="<?php echo $post_type; ?>" | |
> | |
<?php the_title(); ?> | |
</a> | |
</div> | |
<?php endwhile; ?> | |
<?php wp_reset_postdata(); ?> | |
</div> | |
<?php endif; ?> | |
</div> | |
<div class="col-md-4"> | |
<div id="ajaxResponse" class="d-none"></div> | |
</div> | |
</div> | |
</div> | |
<?php get_footer(); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment