Robert Granlund
asked on
AJAX pass along a variable
I have an AJAX call that works fine. I would like to recycle it and pass along a variable and am not 100% sure on how to accomplish this.
If I have Wordpress webpage and I have the category in a Variable $cat = "news". I would like to pass it to the AJAX. What I have so far:
And then The PHP. Please notice on line:7 is where I need to insert the Variable
If I have Wordpress webpage and I have the category in a Variable $cat = "news". I would like to pass it to the AJAX. What I have so far:
<script>
jQuery(function ($) {
$('.post-listing').append('<span class="load-more"></span>');
var button = $('.post-listing .load-more');
var page = 2;
var loading = false;
var scrollHandling = {
allow: true,
reallow: function () {
scrollHandling.allow = true;
}
};
$(window).scroll(function () {
if (!loading && scrollHandling.allow) {
scrollHandling.allow = false;
setTimeout(scrollHandling.reallow, scrollHandling);
var offset = $(button).offset().top - $(window).scrollTop();
if (700 > offset) {
loading = true;
$('#loading-image').bind('ajaxStart', function(){
$(this).show();
}).bind('ajaxStop', function(){
$(this).hide();
});
var data = {
action: 'be_ajax_load_more',
page: page,
query: beloadmore.query,
};
$.post(beloadmore.url, data, function (res) {
if (res.success) {
$('.post-listing').append(res.data);
$('.post-listing').append(button);
page = page + 1;
loading = false;
} else {
//console.log(res);
// console.log('hi');
}
}).fail(function (xhr, textStatus, e) {
//console.log(xhr.responseText);
//console.log('fail');
});
}
}
});
});
And then The PHP. Please notice on line:7 is where I need to insert the Variable
<?php
function be_ajax_load_more() {
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'category_name' => $cat,
'post_status' => 'publish'
);
ob_start();
$posts_array = get_posts($args);
$kount = count($posts_array);
$kount3 = $kount - ($kount % 3);
$posts_array = array_slice($posts_array, 0, $kount3);
global $post;
foreach ($posts_array as $post) {
$field_name = "show_video";
$id = $post->ID;
$video = get_field($field_name, $id);
$category = $category[0]->cat_name;
$title = $post->post_title;
$title = mb_strimwidth($title, 0, 50, '...');
$tumbnail = get_the_post_thumbnail($id);
$category = get_the_category();
?>
<div class="col-md-4 col-sm-6 col-xs-12">
<div id="post-<?php echo $id; ?>" <?php post_class(); ?>>
<div class="item" onclick="document.location.href = '<?php the_permalink($post->ID); ?>'; return false">
<div class="thumbnail">
<?php
if (!empty($video)) {
echo '<div class="video-play">
<img src="../news/wp-content/themes/surfline/img/playbtn.svg" alt="video" class="video-play-controller">
</div>';
}
?>
<?php
the_post_thumbnail($id);
?> </div>
<div class="meta">
<?php
foreach ($category as $c) {
$cat_name = $c->cat_name;
$cat_slug = $c->slug;
if ($cat_name != "All") {
if ($cat_name != "Curated") {
echo '<a href="../' . $cat_slug. '">' . $cat_name . '</a> ';
}
}
}
?>
<span><i class="fa fa-clock-o" aria-hidden="true"></i> <?php echo get_the_date('Y-m-d'); ?>
<?php
$before = "| <strong>Updated</strong> ";
if (get_the_modified_time('U') != get_the_time('U')) {
echo $before;
echo human_time_diff(get_the_modified_date('U'), current_time('timestamp')) . ' ' . __('ago');
}
?>
</span>
</div>
<?php echo'<div class="headline">' . $title . '</div>'; ?>
</div>
</div>
</div>
<?php
}
// END Foreach
wp_reset_postdata();
$data = ob_get_clean();
wp_send_json_success($data);
wp_die();
}
add_action('wp_ajax_be_ajax_load_more', 'be_ajax_load_more');
add_action('wp_ajax_nopriv_be_ajax_load_more', 'be_ajax_load_more');
function be_load_more_js() {
global $wp_query;
$args = array(
'url' => admin_url('admin-ajax.php'),
'query' => $wp_query->query,
);
wp_enqueue_script('be-load-more', get_stylesheet_directory_uri() . '/js/load-more.js', array('jquery'), '1.0', true);
wp_localize_script('be-load-more', 'beloadmore', $args);
}
add_action('wp_enqueue_scripts', 'be_load_more_js');
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER