douliang2935 2017-03-08 04:11
浏览 80
已采纳

使用WP-pagenavi和AJAX的WordPress页面导航

I am trying to get page navigation going, for posts which are displayed as an ajax response.

I'm not sure exactly how to accomplish this.

Here is some code to look at.

$posts_per_page = 6;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$pageposts = query_posts(
    array(
    'post_type' => 'post',
    'cat' => intval($_POST['pageCategory']),
    'paged' => $paged,
    'posts_per_page' => $posts_per_page,
));


if ($pageposts):
    $response = '';

    foreach ($pageposts as $post):
        $postId = get_the_ID();
        $postPermalink = get_the_permalink($postId);
        $postTitle = get_the_title();

        $response .= '
        <article class="gridView col-lg-4 col-md-6 col-xs-12">
            <div class="list-article-thumb" style="background: url('; if ( get_the_post_thumbnail_url() == false ) { $response .= get_stylesheet_directory_uri() . '/images/placholder2.png'; } else { $response .= get_the_post_thumbnail_url(); } $response .= ') no-repeat; height: 445px; background-size: cover; position: relative;">

            </div>

        </article>
        ';
    endforeach;

    $response .= wp_pagenavi();
    wp_reset_query();

else :
    $response = '
    <h2 class="center">Not Found</h2>
    <p class="center">Sorry, but you are looking for something that isn\'t here.</p>
    ';
endif;

The page navigation is returned as part of the ajax request but when I click on the next page button it goes to that page, rather than sending another request to ajax-posts.php

Cheers ol mates

  • 写回答

1条回答 默认 最新

  • dongmu1390 2017-03-08 05:05
    关注

    OK so it seems using WP-pagenavi was pointless, was easier to build my own solution. It uses infinite scrolling.

    Here is what I have done:

    1. Initially I send the ajax request with a limit of 6 posts.

      var pageCategory = "' . $categoryId . '";
      $.post("/testwp2/wp-content/themes/my-theme/ajax-posts.php", {
          initialize: true, pageCategory: pageCategory, resultLimit: 6
      }).done(function(data) {
          $("#primary #main").html(data);
      });
      
    2. On the ajax-posts.php page I process the request using the new limit var and include some jquery in the response.

      global $wpdb;
      global $post;
      
      $limit = $_POST['resultLimit'];
      
      $querystr = "SELECT {$wpdb->prefix}posts.ID, {$wpdb->prefix}posts.post_title, {$wpdb->prefix}posts.post_excerpt, {$wpdb->prefix}posts.guid, {$wpdb->prefix}posts.post_date, {$wpdb->prefix}posts.post_author
      FROM {$wpdb->prefix}posts
      LEFT JOIN {$wpdb->prefix}term_relationships rel ON rel.object_id = {$wpdb->prefix}posts.ID
      LEFT JOIN {$wpdb->prefix}term_taxonomy tax ON tax.term_taxonomy_id = rel.term_taxonomy_id
      LEFT JOIN {$wpdb->prefix}terms t ON t.term_id = tax.term_id
      WHERE t.term_id = " . $_SESSION['pageCategory'] . "
      AND {$wpdb->prefix}posts.post_type = 'post' 
      ORDER BY {$wpdb->prefix}posts.post_date DESC
      LIMIT $limit
      ";
      
      $pageposts = $wpdb->get_results($querystr, OBJECT);
      
      
      if ($pageposts):
      $response = '';
      
      foreach ($pageposts as $post):
          $postId = get_the_ID();
          $postPermalink = get_the_permalink($postId);
          $postTitle = get_the_title();
      
          $response .= '
          <article class="gridView col-lg-4 col-md-6 col-xs-12">
              <div class="list-article-thumb" style="background: url('; if ( get_the_post_thumbnail_url() == false ) { $response .= get_stylesheet_directory_uri() . '/images/placholder2.png'; } else { $response .= get_the_post_thumbnail_url(); } $response .= ') no-repeat; height: 445px; background-size: cover; position: relative;">
      
              </div>
      
          </article>
          ';
      endforeach;
      //This is the jquery to make infinite scroll happen
      $response .= '
          <script type="text/javascript">
              jQuery(document).ready(function($) {
                  $(window).scroll(function() {
                      if($(window).scrollTop() + $(window).height() - 50 == $(document).height() - 50) {
                          var pageCategory = ' . $_SESSION['pageCategory'] . '
                          $.post("/testwp2/wp-content/themes/my-theme/ajax-posts.php", {
                              initialize: true, pageCategory: pageCategory, resultLimit: 6 + ' . $limit . '
                          }).done(function(data) {
                              $("#primary #main").html(data);
                          });
                          alert($limit);
                      }
                  });
              });
          </script>
      ';
      
      wp_reset_query();
      
      else :
      $response = '
      <h2 class="center">Not Found</h2>
      <p class="center">Sorry, but you are looking for something that isn\'t here.</p>
      ';
      endif;
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?