-
-
Save ramzs/e133070bd8e90c71965591d7df6f2e90 to your computer and use it in GitHub Desktop.
Динамическая подгрузка постов wordPress
This file contains hidden or 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 true_load_posts(){ | |
$args = unserialize(stripslashes($_POST['query'])); | |
$args['paged'] = $_POST['page'] + 1; // следующая страница | |
$args['post_status'] = 'publish'; | |
$q = new WP_Query($args); | |
if( $q->have_posts() ): | |
while($q->have_posts()): $q->the_post(); | |
/* | |
* Со строчки 13 по 27 идет HTML шаблон поста, максимально приближенный к теме TwentyTen. | |
* Для своей темы вы конечно же можете использовать другой код HTML. | |
*/ | |
?> | |
<div class="wrap_work mix<?php | |
$tags = wp_get_post_tags( $q->post->ID ); | |
if ($tags) { | |
foreach ($tags as $tag) { | |
echo ' ' . $tag->name; | |
} | |
} | |
?>" style="display: inline-block" data-myorder="<?php echo $q->post->ID ?>"> | |
<?php the_post_thumbnail( array( 364, 364 )); ?> | |
<a href="#" class="animated fadeIn"> | |
<strong><?php the_title(); ?></strong> | |
<?php the_content() ?> | |
<span class="btn_y"><?php echo get_post_meta ( $q->post->ID, 'more', true ); ?></span> | |
</a> | |
</div> | |
<?php | |
endwhile; | |
endif; | |
wp_reset_postdata(); | |
die(); | |
} | |
add_action('wp_ajax_loadmore', 'true_load_posts'); | |
add_action('wp_ajax_nopriv_loadmore', 'true_load_posts'); |
This file contains hidden or 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
<? $countcat = get_category(10 ,false); ?> | |
<?php if ( $countcat->count > 9 ) : ?> | |
<script> | |
var ajaxurl = '<?php echo site_url() ?>/wp-admin/admin-ajax.php'; | |
var true_posts = '<?php echo serialize($wp_query->query_vars); ?>'; | |
var current_page = <?php echo (get_query_var('paged')) ? get_query_var('paged') : 1; ?>; | |
var max_pages = '<?php echo $wp_query->max_num_pages; ?>'; | |
</script> | |
<div id="true_loadmore" class="btn_y">Загрузить ещё</div> | |
<?php endif; ?> |
This file contains hidden or 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
jQuery(function($){ | |
$('#true_loadmore').click(function(){ | |
$(this).text('Загружаю...'); // изменяем текст кнопки, вы также можете добавить прелоадер | |
var data = { | |
'action': 'loadmore', | |
'query': true_posts, | |
'page' : current_page | |
}; | |
$.ajax({ | |
url:ajaxurl, // обработчик | |
data:data, // данные | |
type:'POST', // тип запроса | |
success:function(data){ | |
if( data ) { | |
$('#true_loadmore').text('Загрузить ещё').before(data); // вставляем новые посты | |
current_page++; // увеличиваем номер страницы на единицу | |
if (current_page == max_pages) $("#true_loadmore").remove(); // если последняя страница, удаляем кнопку | |
} else { | |
$('#true_loadmore').remove(); // если мы дошли до последней страницы постов, скроем кнопку | |
} | |
$('.filter, .wrap_work').on('click', 'a', function(e){ | |
e.preventDefault(); | |
}); | |
} | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment