如何在我的侧边栏中显示浏览量最大的帖子?

时间:2012-03-23 作者:Terrell Anderson

嘿,我想在侧边栏中显示5个最受欢迎的按视图计数的帖子,但我不想使用插件,我想要php函数

这样我就可以手动编辑它,比如在标题旁边添加缩略图,知道在哪里可以找到这些代码吗

1 个回复
SO网友:Bainternet

根据@userabuser评论:

<?php
/*
Plugin Name: Simple Post Views
Plugin URI: http://en.bainternet.info
Description: Simple And lightweight plugin to track post views.
Version: 0.1
Author: Bainternet
Author URI: http://en.bainternet.info
*/
if ( !class_exists(\'PostViews\')){
    class PostViews{

        static $meta_key = "_post_views_count"

        //class constractor
        public function __construct(){
            //auto count view on the_content function.
            add_filter(\'the_content\',array($this,\'setPostViews\'));
            //add shortcodes
            add_shortcode("TOP_VIEWED",array($this,\'top_viewed\'));
            add_shortcode("GET_VIEWS"array($this,\'top_viewed\'));
            //add colums to list view
            add_filter(\'manage_posts_columns\', array($this,\'posts_columns_id\'), 5);
            add_action(\'manage_posts_custom_column\', array($this,\'posts_custom_id_columns\'), 5, 2);
        }

        //add views column to posts listing
        public function posts_columns_id($defaults){
            $defaults[\'post_views\'] = __(\'Views\');
            return $defaults; 
        }

        //render views column
        public function posts_custom_id_columns($column_name, $id){
            if($column_name === \'post_views\'){
                echo $this->getPostViews(get_the_ID());
            }
        }

        //function to get the views
        public function getPostViews($postID = null){
            if (null == $postID){
                global $post;
                $postID = $post->ID;
            }

            $count = get_post_meta($postID, $this->meta_key, true);
            if($count==\'\'){
                delete_post_meta($postID, $this->meta_key);
                add_post_meta($postID, $this->meta_key, \'0\');
                $re = 0; 
            }else{
                $re = $count;
            }
            return apply_filters("Post_Views_get_views",$re,$postID);
        }

        //function to set the view count
        public function setPostViews($postID = null) {
            if (null == $postID){
                global $post;
                $postID = $post->ID;
            }

            $count = get_post_meta($postID, $this->meta_key, true);
            if($count==\'\'){
                $count = 0;
                delete_post_meta($postID, $this->meta_key);
                add_post_meta($postID, $this->meta_key, \'0\');
            }else{
                $count++ ;
                update_post_meta($postID, $this->meta_key, $count); 
            }
        }

        //function to get views by shortcode.
        public function top_viewed($atts, $content = null) {
            extract(shortcode_atts(array(
            "id" => null
            ), $atts));
            return $this->getPostViews($id);
        }

        /**
         * shortcode handler for top viewed
         * usage: [TOP_VIEWED count="5"]
         * this will list top five viewed posts
         */
        public function top_viewed($atts, $content = null) {
            extract(shortcode_atts(array(
            "count" => \'5\'
            ), $atts));

            global $wp_query,$paged,$post;
            $temp = $wp_query;
            $wp_query= null;
            $wp_query = new WP_Query(array(
                \'posts_per_page\' => $count,
                \'order\' => \'DESC\',
                \'orderby\' => \'meta_value_num\',
                \'meta_key\' => $this->meta_key
            ));
            ob_start();
            ?>
                <ul class="loop">
                <?php have_posts()) : $wp_query->the_post(); ?>
                <li><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></li>
                <?php endwhile; ?>
                </ul>
            <?php
            $wp_query = null;
            $wp_query = $temp;
            wp_reset_query();
            $content = ob_get_contents();
            ob_end_clean();

            return $content;
        }

    }//end class
}
$Vcounter = new PostViews();

结束

相关推荐

Functions file mods and CPU

将添加类似于我在下面粘贴到函数的代码。php主题文件会降低Wordpress站点的速度,还是影响CPU?(谢谢)function remove_menu_items() { global $menu; $restricted = array(__(\'Links\'), __(\'Comments\'), __(\'Media\'), __(\'Plugins\'), __(\'Tools\'), __(\'Users\')); end ($menu);&#