Skip to content

Instantly share code, notes, and snippets.

@dededey
Created May 23, 2016 09:37
Show Gist options
  • Save dededey/42870d9ca8d677dc9dda4cb6f09f0d32 to your computer and use it in GitHub Desktop.
Save dededey/42870d9ca8d677dc9dda4cb6f09f0d32 to your computer and use it in GitHub Desktop.
wordpress ajax call, load more posts
// chiamata ajax per la pagina category load more posts
function load_old_post()
{
$offset = $_GET['offset'];
$category = $_GET['category'];
$args = array( 'posts_per_page' => 2 , 'cat' => $category, 'offset' => $offset );
$theme_post_query = new WP_Query( $args );
while( $theme_post_query->have_posts() ) : $theme_post_query->the_post();?>
<?php $category = get_the_category(); $category = $category[0]; ?>
<?php $parent_cat = get_cat_name( $category->parent ); ?>
<?php
$fields = get_fields();
$luogo = $fields['luogo'];
$url = get_the_permalink();
?>
<article class="<?php if($parent_cat != NULL) : echo strtolower($parent_cat); else : echo $category->slug; endif; ?> cf">
<a href="<?php echo the_permalink(); ?>">
<!-- post thumbnail -->
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<figure><?php the_post_thumbnail(array(900,600));?></figure>
<?php else : ?> <figure><img src="<?php echo get_template_directory_uri(); ?>/img/surfer.jpg" alt="" title="" /></figure>
<?php endif; ?>
<!-- /post thumbnail -->
<div class="date"><span><?php echo $luogo; ?></span> &#126; <?php echo the_time('d • m • Y'); ?></div>
<h2><?php echo get_the_title(); ?></h2>
</a>
<hr />
<div class="description"><?php echo wp_trim_words(get_the_excerpt(),20 ); ?></div>
<footer class="article-social cf">
<a target="_blank" href="<?php echo fbShare(get_the_permalink(),wp_get_attachment_url( get_post_thumbnail_id())); ?>" class="facebook-link"><span>facebook</span></a>
<a target="_blank" href="<?php echo twitterShare(get_the_permalink()); ?>" class="twitter-link "><span>twitter</span></a>
<a target="_blank" href="<?php echo pinterestShare(get_the_permalink(),$thumbnail_url,$title); ?>" class="pinterest-link"><span>pinterest</span></a>
</footer>
</article>
<?php endwhile;
exit;
}
add_action('wp_ajax_load_old_post', 'load_old_post');
add_action('wp_ajax_nopriv_load_old_post', 'load_old_post');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment