Add this snippet to functions.php
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');
Displaying Popular Posts
<h3>Legnépszerűbb bejegyzések</h3> <?php $args = array( 'posts_per_page'=>3, 'meta_key'=>'popular_posts', 'orderby'=>'meta_value_num', 'order'=>'DESC' ); $popular = new WP_Query( $args ); ?> <div class="popular-posts"> <?php while ($popular->have_posts()) : $popular->the_post(); ?> <div class="row"> <div class="col-md-4"> <?php the_post_thumbnail( 'full', array('class' => 'img-responsive ') ); ?> </div> <div class="col-md-8"> <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4> <time class="entry-date"><?php the_date('Y-m-d'); ?></time> </div> </div> <?php endwhile; wp_reset_postdata(); ?> </div>