如何将AJAX添加到我制作的这个分页中?

时间:2019-09-13 作者:MaddoxSt

这是我的函数文件

add_action( \'wp_ajax_homekong_pagination\', \'homekong_pagination\' );
add_action( \'wp_ajax_nopriv_homekong_pagination\', \'homekong_pagination\' ); 
function homekong_pagination($pages = \'\', $range = 2)
{
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == \'\')
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo "<div class=\'paginations\'>";
        echo "<nav class=\'page-navigation\'>";
            echo "<ul id=\'pagination\' class=\'pagination\'>";
                    if($paged > 2 && $paged > $range+1 && $showitems < $pages)
                        echo "<li class=\'page-item\'><a aria-current=\'page\' class=\'page-numbers page-link prev\' href=\'".get_pagenum_link(1)."\'>PREVIOUS</a></li>";
                        if($paged > 1 && $showitems < $pages)
                            echo "<li class=\'page-item\'><a class=\'page-numbers page-link\' href=\'".get_pagenum_link($paged - 1)."\'><span class=\'dashicons dashicons-arrow-left-alt\'></span></a></li>";
                        for ($i=1; $i <= $pages; $i++)
                        {
                            if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
                                {
                                    echo ($paged == $i)? "<li class=\'page-item\'><a class=\'page-numbers page-link current\'>".$i."</a></li>" : "<li class=\'page-item\'><a class=\'page-numbers page-link\' href=\'".get_pagenum_link($i)."\'>".$i."</a></li>";
                                }
                        }
                        if ($paged < $pages && $showitems < $pages)
                            echo "<li class=\'page-item\'><a class=\'page-numbers page-link\' href=\'" .get_pagenum_link($paged + 1)."\'><span class=\'dashincon dashicons-arrow-right-alt\'></span></a></li>";
                if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages)
                    echo "<li class=\'page-item\'><a class=\'next page-numbers page-link\' href=\'".get_pagenum_link($pages)."\'>NEXT</a></li>";
                echo "</ul>";
            echo "</nav>";
        echo "</div>";
}
exit();
}
这是我希望更改的前端代码。


<div id="blogs" class="blog-items">
    <div class="row">
        <?php
                        // args
                        $paged = get_query_var(\'paged\') ? get_query_var(\'paged\') : 1;
                        $args = array(
                        \'order_by\' => \'publish_date\',
                        \'order\' => \'desc\',
                        \'post_type\' => \'blogs\',
                        \'paged\' => $paged,
                        );
                        // get results
                        $the_query = new WP_Query( $args );
                        // The Loop
                        ?>
        <?php if( $the_query->have_posts() ): ?>
        <?php $counter = 0;?>
        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <div class="col-lg-6">
            <div class="blog-item">
                <div class="img-thumb">
                    <?php the_post_thumbnail();?>
                </div>
                <div class="blog-text">
                    <div class="blog-cat">
                        <label for="">
                            <?php echo blog_categories_terms($post->ID, \'blog-category\');?>
                        </label>
                    </div>
                    <a href="
                        <?php the_permalink();?>">
                        <h3 class="blog-title">
                            <?php the_title();?>
                        </h3>
                    </a>
                    <div class="blog-date">
                        <?php echo get_the_date();?>
                    </div>
                </div>
            </div>
        </div>
        <?php endwhile; ?>
        <?php endif; ?>
        <?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>
        <!-- end row  -->
    </div>
    <!-- end blog items  -->
如何将ajax添加到分页中?我可以;我想不出办法

1 个回复
SO网友:Alex Vasilyev

要做到这一点,您只需要在javascript中发出一个ajax请求。

下面是一个逐步解决方案。

在javascript中添加单击处理程序

单击将ajax请求发送到分页页面时

用新页面中当前页面上的“.blog items”选择器替换元素

jQuery(document.body).on(\'click\', \'#pagination a\', function (event) {
   var linkElement = jQuery(event.currentTarget);

   jQuery.ajax(linkElement.attr(\'href\')).done(function (newPageHTML) {

      jQuery(\'.blog-items\').replaceWith(
          jQuery(newPageHTML).find(\'.blog-items\')
      );

});

相关推荐

如何使用AJAX调用带参数的php页面

我使用位计算,以便通过php文件显示计算值,以便我尝试实现AJAX代码,但URL未加载php文件。。,有什么建议吗。。?function fun5() { var id1=document.getElementById(\'city\').value; $.ajax({ url:\'loaddata.php?id1=\'+id1, complete:function(data) {