-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathview-popular-posts.php
41 lines (37 loc) · 1.13 KB
/
view-popular-posts.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
/*--------------------------------------------------------------
Exibe Posts Populares
--------------------------------------------------------------*/
function wpb_set_post_views( $postID ) {
$count_key = 'wpb_post_views_count';
$count = get_post_meta( $postID, $count_key, true );
if( $count=='' ){
$count = 0;
delete_post_meta( $postID, $count_key );
add_post_meta( $postID, $count_key, '0' );
}else{
$count++;
update_post_meta( $postID, $count_key, $count );
}
}
// Limpando a pré-busca para manter a contagem exata
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
?>
<!-- INSERT SINGLE -->
<?php get_header();
wpb_set_post_views( get_the_ID() );
?>
<!-- INSERT LOOP WORDPRESS -->
<?php
$popularPost = new WP_Query(
array(
'category_name' => 'nome-categoria',
'posts_per_page' => 3,
'meta_key' => 'wpb_post_views_count',
'orderby' => 'meta_value'
)
);
?>
<?php while ( $popularPost->have_posts() ) : $popularPost->the_post(); ?>
<!-- content -->
<?php endwhile; wp_reset_postdata(); ?>