I have a load-more.js file the entire code is here →
jQuery(function($){
$('.class1').append( '<span class="load-more"></span>' );
var button = $('.class1 .load-more');
var page = 2;
var loading = false;
var scrollHandling = {
allow: true,
reallow: function() {
scrollHandling.allow = true;
},
delay: 400 //(milliseconds) adjust to the highest acceptable value
};
$(window).scroll(function(){
if( ! loading && scrollHandling.allow ) {
scrollHandling.allow = false;
setTimeout(scrollHandling.reallow, scrollHandling.delay);
var offset = $(button).offset().top - $(window).scrollTop();
if( 2000 > offset ) {
loading = true;
var data = {
action: 'be_ajax_load_more',
nonce: beloadmore.nonce,
page: page,
query: beloadmore.query,
};
$.post(beloadmore.url, data, function(res) {
if( res.success) {
$('.class1').append( res.data );
$('.class1').append( button );
page = page + 1;
loading = false;
} else {
// console.log(res);
}
}).fail(function(xhr, textStatus, e) {
// console.log(xhr.responseText);
});
}
}
});
});
However →
1 →
$('.class1').append( '<span class="load-more"></span>' );
var button = $('.class1 .load-more');
2 →
Needs to be run by a PHp condition because different classes are needed based on the template choosen in the theme customizer.
$('.class1').append( res.data );
$('.class1').append( button );
What I have tried in the load-more.js
:
<?php if ( get_theme_mod( 'template_setting_condition', '' ) === 'option_horizontal' ) ?> {
$('.class1').append( '<span class="load-more"></span>' );
var button = $('.class1 .load-more');
<?php }elseif( get_theme_mod( 'template_setting_condition', '' ) === 'option_gridl' ) { ?>
('.class2').append( '<span class="load-more"></span>' );
var button = $('.class2 .load-more');
<?php } ?>
Everything is in the PHP tag in the .js file.
But this doesn't work. Is it ever possible that php tags can work in the .js
file?
Or my entire effort is in the wrong direction.?