How to make Ajax call with WordPress and hide load more button?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I am learning Javascript in my localhost with WordPress. I have made a AJAX request with wordpress to get some posts on click. And I could successfully done this job. But the problem is I don't want the load more button if there is no post to load. I want to hide the load more button at that time. But I don't know how can I do that.



Here is my code examples:



PHP code block written in function.php file:



wp_localize_script( 'ajax', 'myAjaxObj', array(
'url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'ajax' ),
'posts' => json_encode( $ajaxposts->query_vars ),
'current_page' => get_query_var( 'paged' ) ? get_query_var('paged') : 1,
'max_page' => $ajaxposts->max_num_pages
) );
function get_ajax_posts()
//global $ajaxposts;
check_ajax_referer('ajax', 'security');
$paged = $_POST['page'];
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => $paged
);
$the_query = new WP_Query($args);
//$ajaxposts = new WP_Query($args);
//print_r($ajaxposts->found_posts);

//$response = '';
if( $the_query->have_posts() )
while ( $the_query->have_posts() )
$the_query->the_post();
//get_template_part( 'template-parts/post/content', get_post_format() );
?>
<h1> <?php the_title(); ?> </h1>
<p> <?php the_content(); ?> </p>
<?php


//wp_reset_query();
die();
//wp_die();

add_action('wp_ajax_get_ajax_posts', 'get_ajax_posts');
add_action('wp_ajax_nopriv_get_ajax_posts', 'get_ajax_posts' );


And these code on my template file:



$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => 1,
);
$the_posts = new WP_Query( $args );


if ( $the_posts->have_posts() ) :

/* Start the Loop */
while ( $the_posts->have_posts() ) : $the_posts->the_post();
?>
<h1><?php the_title(); ?></h1>
<p> <?php the_content(); ?> </p>
<?php
endwhile;


This is the javascript code Block



;(function ($) 
$(document).ready(function ()
let page = 2;
$('#loadAjax').click(function ()
let url = myAjaxObj.url;
$('#LoadingImage img').css('display', 'block');
let data =
'action': 'get_ajax_posts',
'query': myAjaxObj.posts,
'page': page,
'security': myAjaxObj.nonce
;
$.post(url, data, function (response)
$('.site-main').append(response);
page++;
$('#LoadingImage img').css('display', 'none');
);
);
);
)(jQuery);









share|improve this question
























  • Here is solution: stackoverflow.com/questions/31713589/…

    – amedv
    Nov 15 '18 at 17:35

















1















I am learning Javascript in my localhost with WordPress. I have made a AJAX request with wordpress to get some posts on click. And I could successfully done this job. But the problem is I don't want the load more button if there is no post to load. I want to hide the load more button at that time. But I don't know how can I do that.



Here is my code examples:



PHP code block written in function.php file:



wp_localize_script( 'ajax', 'myAjaxObj', array(
'url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'ajax' ),
'posts' => json_encode( $ajaxposts->query_vars ),
'current_page' => get_query_var( 'paged' ) ? get_query_var('paged') : 1,
'max_page' => $ajaxposts->max_num_pages
) );
function get_ajax_posts()
//global $ajaxposts;
check_ajax_referer('ajax', 'security');
$paged = $_POST['page'];
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => $paged
);
$the_query = new WP_Query($args);
//$ajaxposts = new WP_Query($args);
//print_r($ajaxposts->found_posts);

//$response = '';
if( $the_query->have_posts() )
while ( $the_query->have_posts() )
$the_query->the_post();
//get_template_part( 'template-parts/post/content', get_post_format() );
?>
<h1> <?php the_title(); ?> </h1>
<p> <?php the_content(); ?> </p>
<?php


//wp_reset_query();
die();
//wp_die();

add_action('wp_ajax_get_ajax_posts', 'get_ajax_posts');
add_action('wp_ajax_nopriv_get_ajax_posts', 'get_ajax_posts' );


And these code on my template file:



$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => 1,
);
$the_posts = new WP_Query( $args );


if ( $the_posts->have_posts() ) :

/* Start the Loop */
while ( $the_posts->have_posts() ) : $the_posts->the_post();
?>
<h1><?php the_title(); ?></h1>
<p> <?php the_content(); ?> </p>
<?php
endwhile;


This is the javascript code Block



;(function ($) 
$(document).ready(function ()
let page = 2;
$('#loadAjax').click(function ()
let url = myAjaxObj.url;
$('#LoadingImage img').css('display', 'block');
let data =
'action': 'get_ajax_posts',
'query': myAjaxObj.posts,
'page': page,
'security': myAjaxObj.nonce
;
$.post(url, data, function (response)
$('.site-main').append(response);
page++;
$('#LoadingImage img').css('display', 'none');
);
);
);
)(jQuery);









share|improve this question
























  • Here is solution: stackoverflow.com/questions/31713589/…

    – amedv
    Nov 15 '18 at 17:35













1












1








1








I am learning Javascript in my localhost with WordPress. I have made a AJAX request with wordpress to get some posts on click. And I could successfully done this job. But the problem is I don't want the load more button if there is no post to load. I want to hide the load more button at that time. But I don't know how can I do that.



Here is my code examples:



PHP code block written in function.php file:



wp_localize_script( 'ajax', 'myAjaxObj', array(
'url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'ajax' ),
'posts' => json_encode( $ajaxposts->query_vars ),
'current_page' => get_query_var( 'paged' ) ? get_query_var('paged') : 1,
'max_page' => $ajaxposts->max_num_pages
) );
function get_ajax_posts()
//global $ajaxposts;
check_ajax_referer('ajax', 'security');
$paged = $_POST['page'];
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => $paged
);
$the_query = new WP_Query($args);
//$ajaxposts = new WP_Query($args);
//print_r($ajaxposts->found_posts);

//$response = '';
if( $the_query->have_posts() )
while ( $the_query->have_posts() )
$the_query->the_post();
//get_template_part( 'template-parts/post/content', get_post_format() );
?>
<h1> <?php the_title(); ?> </h1>
<p> <?php the_content(); ?> </p>
<?php


//wp_reset_query();
die();
//wp_die();

add_action('wp_ajax_get_ajax_posts', 'get_ajax_posts');
add_action('wp_ajax_nopriv_get_ajax_posts', 'get_ajax_posts' );


And these code on my template file:



$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => 1,
);
$the_posts = new WP_Query( $args );


if ( $the_posts->have_posts() ) :

/* Start the Loop */
while ( $the_posts->have_posts() ) : $the_posts->the_post();
?>
<h1><?php the_title(); ?></h1>
<p> <?php the_content(); ?> </p>
<?php
endwhile;


This is the javascript code Block



;(function ($) 
$(document).ready(function ()
let page = 2;
$('#loadAjax').click(function ()
let url = myAjaxObj.url;
$('#LoadingImage img').css('display', 'block');
let data =
'action': 'get_ajax_posts',
'query': myAjaxObj.posts,
'page': page,
'security': myAjaxObj.nonce
;
$.post(url, data, function (response)
$('.site-main').append(response);
page++;
$('#LoadingImage img').css('display', 'none');
);
);
);
)(jQuery);









share|improve this question
















I am learning Javascript in my localhost with WordPress. I have made a AJAX request with wordpress to get some posts on click. And I could successfully done this job. But the problem is I don't want the load more button if there is no post to load. I want to hide the load more button at that time. But I don't know how can I do that.



Here is my code examples:



PHP code block written in function.php file:



wp_localize_script( 'ajax', 'myAjaxObj', array(
'url' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'ajax' ),
'posts' => json_encode( $ajaxposts->query_vars ),
'current_page' => get_query_var( 'paged' ) ? get_query_var('paged') : 1,
'max_page' => $ajaxposts->max_num_pages
) );
function get_ajax_posts()
//global $ajaxposts;
check_ajax_referer('ajax', 'security');
$paged = $_POST['page'];
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => $paged
);
$the_query = new WP_Query($args);
//$ajaxposts = new WP_Query($args);
//print_r($ajaxposts->found_posts);

//$response = '';
if( $the_query->have_posts() )
while ( $the_query->have_posts() )
$the_query->the_post();
//get_template_part( 'template-parts/post/content', get_post_format() );
?>
<h1> <?php the_title(); ?> </h1>
<p> <?php the_content(); ?> </p>
<?php


//wp_reset_query();
die();
//wp_die();

add_action('wp_ajax_get_ajax_posts', 'get_ajax_posts');
add_action('wp_ajax_nopriv_get_ajax_posts', 'get_ajax_posts' );


And these code on my template file:



$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => 1,
);
$the_posts = new WP_Query( $args );


if ( $the_posts->have_posts() ) :

/* Start the Loop */
while ( $the_posts->have_posts() ) : $the_posts->the_post();
?>
<h1><?php the_title(); ?></h1>
<p> <?php the_content(); ?> </p>
<?php
endwhile;


This is the javascript code Block



;(function ($) 
$(document).ready(function ()
let page = 2;
$('#loadAjax').click(function ()
let url = myAjaxObj.url;
$('#LoadingImage img').css('display', 'block');
let data =
'action': 'get_ajax_posts',
'query': myAjaxObj.posts,
'page': page,
'security': myAjaxObj.nonce
;
$.post(url, data, function (response)
$('.site-main').append(response);
page++;
$('#LoadingImage img').css('display', 'none');
);
);
);
)(jQuery);






javascript php wordpress






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 15:48







Rajan Karmaker

















asked Nov 15 '18 at 15:34









Rajan KarmakerRajan Karmaker

63




63












  • Here is solution: stackoverflow.com/questions/31713589/…

    – amedv
    Nov 15 '18 at 17:35

















  • Here is solution: stackoverflow.com/questions/31713589/…

    – amedv
    Nov 15 '18 at 17:35
















Here is solution: stackoverflow.com/questions/31713589/…

– amedv
Nov 15 '18 at 17:35





Here is solution: stackoverflow.com/questions/31713589/…

– amedv
Nov 15 '18 at 17:35












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322827%2fhow-to-make-ajax-call-with-wordpress-and-hide-load-more-button%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322827%2fhow-to-make-ajax-call-with-wordpress-and-hide-load-more-button%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Darth Vader #20

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Ondo