web-dev-qa-db-ja.com

サイドバーに最も表示されている投稿を表示する方法

ちょっと私は私のサイドバーにビューの数によって5最も人気のある記事を表示したいと思ったが、私はプラグインを使用したくない、私はphp関数が欲しい

タイトルの横にサムネイルを追加するなど、手動で編集できるように、このコードを見つけることができる任意のアイデア

1

@ 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();
1
Bainternet