Custom loop and duplicate post
-
Hi, i’ve sort-of written a loop to get some pagination going on, but whenever the loop reaches the end, the last post will be repeated (if it’s not an odd post count).
The “pagination” system is a basic ajax query:
<?php function charger_articles() { $offset = $_POST['offset']; $args = array( 'category_name'=>get_the_title(), 'post_type' => 'post', 'offset' => $offset, 'posts_per_page' => '2', ); $ajax_query = new WP_Query($args); //var_dump($ajax_query); if ( $ajax_query->have_posts() ) { while ( $ajax_query->have_posts() ) { $ajax_query->the_post(); echo '<div class=\'categorieMiniArticle\'>'; echo '<div class=\'col-xs-12 col-md-6\' max_height=\'300px\'>'; echo '<div class=\'miniatureContainer\'>'; he_post_thumbnail( 'moitie',array( 'class' => 'img-responsive', )); echo '</div>'; echo '<div class=\'miniArticlesContainer\'>'; echo '<a>'; echo '<h2>' . get_the_title() . '</h2>'; echo '<h3>' . get_the_excerpt() . '</h3>'; echo '</a>'; echo '</div>'; echo '</div>'; echo '</div>'; } echo '<div class=\'moreposthere\'></div>'; } die(); } ?>And the ajax request:
var offset = 3; jQuery('body').on('click', '.load_more', function() { jQuery.post( ajaxurl, { 'action': 'charger_articles', 'offset': offset }, function(response) { if (response) { offset= offset + 2; // console.log(response); jQuery( ".moreposthere" ).append(response); } } ); });offset is set to 3 because my page has 3 articles shown before the ajax request can be made, and than i want it to add two articles per click on the button. It does work, but when it reaches a point where it can only find one, cause offset = numberOfArticles – 1; it will add twice the same article
Can someone provide some help?
The topic ‘Custom loop and duplicate post’ is closed to new replies.