php - wordpress ajax pagination

admin2025-01-07  5

I'm working on a WordPress Theme (create a custom theme) and using AJAX as pagination for my Custom Posts.

So I'm going to load pages with AJAX but the problem is when I click for next page, instead of next page it's returning the home page.

Now I'm going to make it easier with a simple sample, if you can help me to call HELLO query in sample below, I will fix the whole codes with your path.

Here is my HELLO target for example (we should call it):

This is my code in functions.php

add_action("wp_ajax_check_user", "check_user");
add_action("wp_ajax_nopriv_check_user", "check_user");

function check_user() {

    echo 'hello';
    die();
}

And this is my code to call "HELLO" in some page:

$.ajax({
        type: 'POST',
        url: "<?php echo admin_url('admin-ajax.php') ?>",
        data: { 
            'action': 'check_user'
            },
        success : function(data) {
            alert(data); //FOR DEBUG
        }

    });

So I can't call hello and instead I see my home page.


UPDATE: @face

this is my sample page and you can see the results after click on next page or page 2:

here is my sample page

at the bottom of the page please try to go next page or page 2. i made the results as a message box.

I'm working on a WordPress Theme (create a custom theme) and using AJAX as pagination for my Custom Posts.

So I'm going to load pages with AJAX but the problem is when I click for next page, instead of next page it's returning the home page.

Now I'm going to make it easier with a simple sample, if you can help me to call HELLO query in sample below, I will fix the whole codes with your path.

Here is my HELLO target for example (we should call it):

This is my code in functions.php

add_action("wp_ajax_check_user", "check_user");
add_action("wp_ajax_nopriv_check_user", "check_user");

function check_user() {

    echo 'hello';
    die();
}

And this is my code to call "HELLO" in some page:

$.ajax({
        type: 'POST',
        url: "<?php echo admin_url('admin-ajax.php') ?>",
        data: { 
            'action': 'check_user'
            },
        success : function(data) {
            alert(data); //FOR DEBUG
        }

    });

So I can't call hello and instead I see my home page.


UPDATE: @face

this is my sample page and you can see the results after click on next page or page 2:

here is my sample page

at the bottom of the page please try to go next page or page 2. i made the results as a message box.

Share Improve this question edited Sep 26, 2016 at 13:41 ehsandesign asked Sep 26, 2016 at 9:38 ehsandesignehsandesign 13 bronze badges 3
  • Can you paste the result of your ajax call please? – FaCE Commented Sep 26, 2016 at 11:29
  • @FaCE original post updatet. please take a look – ehsandesign Commented Sep 26, 2016 at 13:45
  • Ok, I've had a look -- what plugins are you using? For a start your URLs aren't ending with trailing slashes, and when I go to yoursamplepage.com/wp-admin/ I get your homepage returned -- try disabling any plugins that affect your permalinks and try your script again. – FaCE Commented Sep 26, 2016 at 16:19
Add a comment  | 

1 Answer 1

Reset to default 0

Is your "next" Button an HTML "a"-Element? Please add a little bit more of your code. How do you call your $.ajax - Method?

Am I right that you call it sometings like this:

$("#elementid").on("click", function() {
   $.ajax({....});
}

When I am right and you use an a-Element. Add the following after the "click" method:

$("#elementid").on("click", function(e) {
    e.preventDefault();
    $.ajax({....});
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736254851a237.html

最新回复(0)