i have site on wp.
i'm using ajax show posts. on click on 'more' button appears 2 more posts. , i'm trying hide button when of posts shown. go wrong.
i don't understand how take posts count js (it's variant), because construction var posts_count = "<?php echo $posts_count; ?>
doesn't work - gives string!
js
$(function () { var posts = 2; //posts shown var posts_offset = 2; $("#load-post").click(function (e) { e.preventdefault(); $.ajax({ type: "get", url: "/wp-admin/admin-ajax.php", data: ({ action: 'loadmore', posts_offset: posts_offset }), success: function (data) { $('.news #content').append(data); posts_offset += 2; } }); }); });
php
function loadmore() { $posts_count = wp_count_posts()->publish; $posts_offset = $_get['posts_offset']; // $_get variables sorted out // setup query want query_posts( array( 'posts_per_page' => 2, 'offset'=> $posts_offset ) ); // initialsise output $output = ''; // loop 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; if ($posts_count - ($posts_offset + 2) <= 0) { ?> <script>$("#load-more").hide();</script> <?php } // reset query wp_reset_query(); die($output); }
this code works, doesn't work part of hiding button
change code follows:
in php
<script>function hideloadmore(){$("#load-more").hide();}</script>
and in js
success: function (data) { $('.news #content').append(data); posts_offset += 2; if(hideloadmore) { hideloadmore(); } }
Comments
Post a Comment