Using wp_pagenavi for pagination with AJAX loaded content but getting links based on wp-admin/admin-

In crunch time with something and I really need some help. I have a custom template page that has some tabs that dynamically load some ajax content, works great. But the wp_pagenavi() properly displays when needed but does not function properly, I believe because the ?paged=# that is being added to the wrong url.

Rather than domain.com/permalink_path?paged=# I'm getting domain.com/wp-admin/admin-ajax.php?paged=# because I used wordpress's ajax when loading that content.

Thanks for any help you can provide I'm leaving town for a bit and need this fixed before I go so please be quick. Thanks again

Nick

    Div Truth
    • Elite
    • 62
    • #1,995

    Attempting to resolve this myself again I've got this figured out so far. Since the content needs to load in the content div again I was wrong to think I could just change the link, I figured that out and that simply loaded the correct data on a new page. The correct approach is to remove the href from the populated wp_pagenav anchors and then run an onclick method which I've begun like this:

    <script>
    function fixPagination(nonce,category,post,content){
      jQuery("#wp-pagenavi_ .wp-pagenavi a").each(function(){
        jQuery(this).removeAttr("href");
        // jQuery(this).attr('href',jQuery(this).attr('href').replace("/wp-admin/admin-ajax.php?paged=","/category/tv/companies/page/"));
        //answer to original question
    
        jQuery("#wp-pagenavi_ .wp-pagenavi a").click( function() {
          jQuery(".loading").fadeIn();
          jQuery("#content").animate({
            opacity: 0,
            marginLeft: '-=5'
          },250);
    
      // This is what is braking the page right now
        jQuery.ajax({
          dataType : "html",
          url :  "<?php bloginfo('wpurl') ?>/wp-admin/admin-ajax.php",
          data : {action: "get_tv_content", nonce: nonce, content: content, post: post, category: category },
          success: function(response) {
            jQuery(".loading").fadeOut();
            jQuery("#content").html(response);
            jQuery("#content").animate({
              opacity: 1,
    	  marginLeft: '+=5'
            },250);
            jQuery("#content #social-sidebar-container").animate({
      	  marginLeft: '-=5'
            },250);
            //fixPagination();
          },
          error: function(xhr, textStatus, errorThrown){
            alert(errorThrown+"("+xhr+"): " +textStatus);
          }
        })
      })
    })
    	}
    </script>

    If I remove the ajax call made there then everything runs as expected, but as soon as I start that ajax call the entire page freezes and then breaks. Any thoughts would really help I'm running out of other options.

    FYI: action = get_tv_content that is because in this same file I have this defined

    add_action("wp_ajax_".$_REQUEST['action'], $_REQUEST['action']);
    add_action("wp_ajax_nopriv_".$_REQUEST['action'], $_REQUEST['action']);
    
    function get_tv_content(){}
    Arun Basil Lal
    • 1
    • #31,107

    Hey Nick,

    let me pass this onto our dev's, hopefully they can sort something for you.

    Barry

    wp_pagenavi will grab the page that it is running on and use that as the base for the permalinks, as you are finding out.

    If you want to use that function, rather than write your own, then you'll need to edit it to accept a variable / $_GET of the base or page to use as the start of your url, and then update your ajax call to pass over the base or page you are calling from.

    It's not going to be the easiest of things to do, as the plugin looks quite a bit more complex than I'd have expected it to be :)

    Maybe this will provide some help:
    http://www.mygeekpal.com/creating-an-ajax-powered-pagination-in-wordpress/

    PC

    Hello Nick,

    We haven't heard on this one from you and are still waiting for a response on it.

    Please advise

    Cheers
    PC

    Div Truth
    • Elite
    • 62
    • #1,995

    Sorry I got pulled off this task for a bit because I was hitting a dead end. For a temporal solution we made the pagination area a link to the actual page 1 or 2 rather then deal with the ajax at the moment. I will be coming back to this though and I'll be sure to try your suggestion there Barry so thank you.

Join 376705 happy members

Get access to our entire collection of epic plugins, astonishing themes & genius support

Become a member