gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:54:23 24 4
gpt4 key购买 nike

我正在尝试为显示为 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 发送另一个请求

小伙伴们干杯

最佳答案

好吧,看来使用 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;

关于javascript - 使用 WP-pagenavi 和 AJAX 的 WordPress 页面导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42662875/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com