Skip to content

Instantly share code, notes, and snippets.

@Lysindr
Created September 22, 2016 07:33
Show Gist options
  • Save Lysindr/490e018b21c06a121b59891f8b44cc21 to your computer and use it in GitHub Desktop.
Save Lysindr/490e018b21c06a121b59891f8b44cc21 to your computer and use it in GitHub Desktop.
Popular posts #2
//----- Popular post-2
// DIY Popular Posts @ https://digwp.com/2016/03/diy-popular-posts/
function shapeSpace_popular_posts($post_id) {
$count_key = 'popular_posts';
$count = get_post_meta($post_id, $count_key, true);
if ($count == '') {
$count = 0;
delete_post_meta($post_id, $count_key);
add_post_meta($post_id, $count_key, '0');
} else {
$count++;
update_post_meta($post_id, $count_key, $count);
}
}
function shapeSpace_track_posts($post_id) {
if (!is_single()) return;
if (empty($post_id)) {
global $post;
$post_id = $post->ID;
}
shapeSpace_popular_posts($post_id);
}
add_action('wp_head', 'shapeSpace_track_posts');
//Start the loop
<?php
$popular = new WP_Query(array('posts_per_page'=>7, 'meta_key'=>'popular_posts', 'orderby'=>'meta_value_num', 'order'=>'DESC') );
while ($popular->have_posts() ) : $popular->the_post();
?>
<article class="post">
<div class="img-wrapper"><?php the_post_thumbnail(); ?></div>
<h5 class="category-name"><?php the_category(','); ?></h5>
<h3><?php the_title(); ?></h3>
<p>
<?php the_excerpt(); ?>
</p>
<ul class="post-info">
<li><a href="#"><?php the_author(); ?></a></li>
<li><a href="#"><?php echo get_the_date(); ?></a></li>
<li><a href="#"><?php comments_number('No Comments', '1 Comment', '% Comments'); ?></a></li>
</ul>
<a href="<?php the_permalink(); ?>" class="read-more" >Read More</a>
</article>
<? endwhile;
wp_reset_query();
?>
<?php pagenavi(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment