Skip to content

Instantly share code, notes, and snippets.

@timiwahalahti
Last active June 2, 2017 09:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timiwahalahti/57488b9c1e60d1c8772f59601bc7865d to your computer and use it in GitHub Desktop.
Save timiwahalahti/57488b9c1e60d1c8772f59601bc7865d to your computer and use it in GitHub Desktop.
<?php if ( ! is_wp_error( $topics ) && ! empty( $topics ) ) : ?>
<div class="cats" data-filter="cat">
<h4><?php pll_e( 'Valitse aihe' ) ?></h4>
<ul>
<li><a href="<?php echo get_term_link( $term_id ); ?>"<?php echo ( ! isset( $_GET['cat'] ) ) ? ' class="selected"' : ''; ?>><?php _e( 'Kaikki' ) ?></a></li>
<?php foreach ( $topics as $topic ) : ?>
<li>
<a href="<?php echo yeahboy_make_filter_url( $base_url_term_id, 'cat', $topic->term_id ) ?>" data-id="<?php echo $topic->term_id ?>" <?php echo ( $_GET['cat'] == $topic->term_id ) ? ' class="selected"' : ''; ?>>
<?php echo $topic->name ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif;
...
<?php global $wp_query;
if ( $wp_query->found_posts != $wp_query->post_count ) :
$wp_query->query['paged'] = 1; ?>
<div class="button-container">
<p><a href="#" class="button button-ghost load-more" data-use-query="load_more_query"><?php pll_e( 'Lataa lisää' ) ?></a></p>
<script>
var base_category_name = <?php echo json_encode( $wp_query->query['category_name'] ) ?>;
var load_more_query = <?php echo json_encode( $wp_query->query ) ?>;
</script>
</div>
<?php endif; ?>
$('.cats a').on('click', function(e) {
e.preventDefault();
var filter_type = $(this).closest('.cats').attr('data-filter')
var query_name = $('.slide-articles a.load-more').attr('data-use-query');
var query = window[query_name];
$(this).closest('.cats').find('a').removeClass('selected');
$(this).addClass('selected');
// Reset paged for new query.
query.paged = 0;
// Maybe set child category to query.
if ( 'cat' === filter_type ) {
if ( $(this).attr('data-id') == null ) {
delete query.cat;
query.category_name = base_category_name;
} else {
delete query.category_name;
query.cat = $(this).attr('data-id');
}
}
// Hide load more.
$('.slide-articles .button-container').hide();
// Remove old articles.
$('.slide-articles .col').remove();
// Get new articles.
yeahboy_do_ajax_load();
} );
// Load more stuff
// Vue construct
var blog = new Vue({
el: '.slide-articles .cols',
data: {
posts: []
}
});
// Load more ajax call
jQuery('.slide-articles a.load-more').on( 'click', function(e) {
e.preventDefault();
yeahboy_do_ajax_load();
} );
function yeahboy_do_ajax_load() {
var button_container = $('.slide-articles a.load-more').closest('.button-container');
var query_name = $('.slide-articles a.load-more').attr('data-use-query');
var query = window[query_name];
// Alter query
query.paged = query.paged+1;
query._embed = true;
// Do query
$.ajax({
url: '/wp-json/wp_query/args/?' + jQuery.param( query ),
}).done(function( response ) {
if( response.length !== 0 && response !== false ) {
jQuery.each( response, function() {
var self = this;
blog.posts.push(this);
} );
if( response.length < dudeuserrating.posts_per_page ) {
button_container.hide();
} else {
button_container.show();
}
} else if( response == false ) {
button_container.hide();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment