Skip to content

Instantly share code, notes, and snippets.

@chrdesigner
Last active June 14, 2017 16:46
Show Gist options
  • Save chrdesigner/b0e045656b5af591be1b9a073f1fbfa1 to your computer and use it in GitHub Desktop.
Save chrdesigner/b0e045656b5af591be1b9a073f1fbfa1 to your computer and use it in GitHub Desktop.
Git List - Post views Count
<?php
/*
* Set post views count using post meta
*/
function chr_setPostViews($postID) {
$countKey = 'post_views_count';
$count = get_post_meta($postID, $countKey, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $countKey);
add_post_meta($postID, $countKey, '0');
return 'Nenhuma Visualização';
}elseif( is_single($postID) ){
$count++;
update_post_meta($postID, $countKey, $count);
}
return $count . ' Visualização(ões)';
}
<?php
/**
* Add a new column in the wp-admin posts list
*/
function chr_posts_column_views( $defaults ) {
$defaults['post_views'] = __( 'Visualização(ões)' );
return $defaults;
}
/**
* Display the number of views for each posts
*/
function chr_posts_custom_column_views( $column_name, $id ) {
if ( $column_name === 'post_views' ) {
echo chr_setPostViews( get_the_ID() );
}
}
add_filter( 'manage_posts_columns', 'chr_posts_column_views' );
add_action( 'manage_posts_custom_column', 'chr_posts_custom_column_views', 5, 2 );
<?php
/**
* The Template for displaying all single posts.
*
* @package Odin
* @since 2.2.0
*/
get_header(); ?>
<main id="content" class="<?php echo odin_classes_page_sidebar(); ?>" tabindex="-1" role="main">
<?php
// Function for account how many access this post has
chr_setPostViews( get_the_ID() );
// Start the Loop.
while ( have_posts() ) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
endwhile;
?>
</main><!-- #main -->
<?php
get_sidebar();
get_footer();
<?php
// WP_Query arguments
$args = array(
'post_type' => array( 'post' ),
'post_status' => array( 'publish' ),
'orderby' => 'meta_value_num',
'meta_key' => 'post_views_count',
'order' => 'DESC',
'ignore_sticky_posts' => 1,
'posts_per_page' => '4',
);
// The Query
$query_trending = new WP_Query( $args );
$count = 0;
// The Loop
if ( $query_trending->have_posts() ) {
echo '
<article id="box-trending" class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<h5 class="text-center">Trending</h5>
<dl>';
while ( $query_trending->have_posts() ) { $query_trending->the_post(); $count ++;
echo '
<dd title="' . get_the_title() . ' ('. chr_setPostViews( get_the_ID() ) .')">
<span>' . $count . '.</span><h6><a href="' . get_permalink() . '" title="' . get_the_title() . '">' . wp_trim_words( get_the_title(), 6, '...' ) . '</a></h6>
</dd>';
}
} else {
echo '<dd>No posts viewed...</dd>';
}
echo '</dl>
</article>
';
// Restore original Post Data
wp_reset_postdata();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment