javascript - Loading more posts on WP site using AJAX -


i have site on wp. , i'm trying load posts ajax. difference other such problems have no pagination, post section simple: 2 posts near , button below. on click on button 2 posts append previous 2 posts. tried write solution myself, have little expirience in php , maybe in problem.

post structure:

<div class="row">                     <?php if ( have_posts() ) :  while ( have_posts() ) : the_post(); ?>               <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 news-item">               <div class="news-date">                 <?php the_time($format = 'j f y'); ?>               </div>               <div>                 <h3><?php the_title(); ?></h3>                 <p><?php the_content(); ?></p>               </div>             </div>             <?php endwhile; ?>             <?php endif; ?>            </div>            <div class="more-link"><a href="#">read more></a>               </div> 

js

    $(function () {    var posts = 2;   var posts_offset = 0;    $("#load-post").click(function (e) {     e.preventdefault();      $.ajax({       type: "post",       url: "/wp-content/themes/1cka/load-posts.php",       datatype: 'html',       data: {         posts_offset: posts_offset       },       success: function (data) {         $('.news').append(data);         posts_offset += 2;        }     });   }) }); 

php

 <?php require_once("header.php"); ?> <?php if (isset($_get['posts_offset'])) {   $posts_offset = $_get['posts_offset']; }  global $post;  // записываем $post во временную переменную $tmp_post $tmp_post = $post; $args = array( 'posts_per_page' => 2, 'offset'=> $posts_offset ); $myposts = get_posts( $args ); foreach( $myposts $post ) : setup_postdata($post); ?>     <?php if ( have_posts() ) :  while ( have_posts() ) : the_post(); ?>             echo  '<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 news-item">               <div class="news-date">                 <?php the_time($format = 'j f y'); ?>               </div>               <div>                 <h3><?php the_title(); ?></h3>                 <p><?php the_content(); ?></p>               </div>             </div>'             <?php endwhile; ?>             <?php endif; ?> <?php endforeach;   $post = $tmp_post;  ?> 

sorry don't have immediate solution have seen https://github.com/wp-api/wp-api ? implements rest api of wordpress content, able fetch additional posts simple ajax call


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -