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

时间:2017-03-08 04:11:48

标签: javascript php ajax wordpress plugins

我正在尝试进行页面导航,对于显示为ajax响应的帖子。

我不确定如何做到这一点。

以下是一些要查看的代码。

$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;

页面导航作为ajax请求的一部分返回,但当我点击下一页按钮时,它会转到该页面,而不是向ajax-posts.php发送另一个请求

干杯队友

1 个答案:

答案 0 :(得分:0)

好吧所以看起来使用WP-pagenavi毫无意义,更容易构建我自己的解决方案。它使用无限滚动。

这就是我所做的:

  1. 最初我发送的ajax请求限制为6个帖子。

    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. 在ajax-posts.php页面上,我使用新的限制var处理请求,并在响应中包含一些jquery。

    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;