【问题标题】:WordPress page navigation using WP-pagenavi and AJAX使用 WP-pagenavi 和 AJAX 的 WordPress 页面导航
【发布时间】:2017-03-08 04:11:47
【问题描述】:

对于显示为 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 发送另一个请求

朋友们加油

【问题讨论】:

  • “我点击下一页按钮会转到该页面”——您是否停止了默认事件在点击该按钮时触发?
  • 没有页面加载正常,但它带我到页面http://localhost/testwp2/wp-content/themes/my-theme/ajax-posts.php/page/2/ 但该页面不存在,因为 ajax-posts.php 是处理 ajax 请求并返回上述代码的页面$响应变量
  • 我认为我要做的是在响应中使用 js 构建自己的分页,但希望使用插件让生活更轻松。
  • 所以你是说你不想被带到那个页面对吗?您需要 Ajax 继续触发并忽略按钮单击事件吗?
  • 是的,就是这样

标签: javascript php ajax wordpress plugins


【解决方案1】:

好吧,看来使用 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 页面上,我使用新的 limit 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;
    

【讨论】:

  • 嗨!一个问题:正如我所见,您不使用本地 wp ajax 解决方案,例如本地化和调用 admin-ajax.php -built 在核心中。确切的原因是什么?谢谢!最良好的祝愿。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-13
相关资源
最近更新 更多