Last active
March 20, 2018 17:38
-
-
Save marcelo-ribeiro/3cbfc508f74941f362a0d76b77440906 to your computer and use it in GitHub Desktop.
Wordpress - Ajax Pagination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function() { | |
var paged = 1, | |
isLoading = false, | |
posts = $('.posts'), | |
button = $('.read_more'), | |
foundPosts = button.attr('data-found-posts'); | |
(function() { | |
hasMoreItens() && button.fadeIn(); | |
})(); | |
function hasMoreItens() { | |
return !(posts.find('.item').length == foundPosts); | |
} | |
function toggleLoading() { | |
isLoading = !isLoading; | |
if (isLoading) { | |
button.text('Carregando...'); | |
button.attr('disabled', 'disabled'); | |
} else if (!isLoading && hasMoreItens()) { | |
button.text('Ver mais...'); | |
button.removeAttr('disabled'); | |
} | |
} | |
button.click(function() { | |
paged++; | |
toggleLoading(); | |
$.get( | |
themeUrl + 'inc/inc-posts.php', { | |
paged: paged, | |
}, | |
function(data) { | |
posts.append(data); | |
toggleLoading(); | |
!hasMoreItens() && button.fadeOut(); | |
} | |
); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$args = array( | |
'post_type' => 'post', | |
'posts_per_page' => 6, | |
'paged' => 1 | |
); | |
if ( isset($_GET["paged"]) ) { | |
require("../../../../wp-blog-header.php"); | |
header("HTTP/1.1 200 OK"); | |
$args["paged"] = $_GET["paged"]; | |
} | |
$query = new WP_Query($args); | |
$found_posts = $query->found_posts; | |
foreach ($query->posts as $item){ | |
?> | |
<div class="item"> | |
<?php echo $item->post_title; ?> | |
</div> | |
<?php | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<section class="posts"> | |
<?php include ('inc/inc-posts.php'); ?> | |
</section> | |
<button class="button read_more" data-found-posts="<?php echo $found_posts; ?>" style="display:none;">Ver mais...</button> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
blog.php
functions.php
style.css