多姿

  • {{ item.name }}
  • 首页
  • 作品
  • 技巧
  • JWPlayer
  • 友链申请
  • 文章归档

WordPress 实现带图片的月度点击量排行

  • 东少
  • 2014-06-17
  • 0

WordPress 实现带图片的月度点击量排行

在 WordPress 中,我一般用 wp-postviews 插件来统计点击量的多少,这个插件应该是最常用的了,功能很强大,感谢插件的作者。而且插件还自带了一些函数,比如统计最多点击量的文章和页面等,但是你有没有想过输出一些带有图片的点击量排行?可能对于文章内图片比较多的博客比较有用吧。

这个想法已经在之前的 WordPress 免插件实现图片相关日志中有所提及,所以本文的 WordPress 实现的带图片的点击量排行也是依照这个原理,不过这并不是完全非插件的,还是需要 wp-postviews 插件的支持,以此来对点击量进行排行。在看以下代码之前,建议你先看一下 WordPress 的缩略图利器 timthumb 这篇文章,因为在以下的图片缩略图功能借助于 timthumb 这个第三方的缩略图功能。

然后,把以下代码复制到 WordPress 主题的 functions.php 中:

function most_viewed($mode = '', $limit = 10, $display = true) {
    global $wpdb, $post;
    $where = '';
    $temp = '';
    $output = '';
    if(!empty($mode) && $mode != 'both') {
        $where = "post_type = '$mode'";
    } else {
        $where = '1=1';
    }
    $most_viewed = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID WHERE post_date > '" . date('Y-m-d', strtotime('-30 days')) . "' AND $where AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER BY views DESC LIMIT $limit");
    if($most_viewed) {
        foreach ($most_viewed as $post) {
            $post_title = get_the_title();
            if(has_post_thumbnail( intval($post->ID) )){
                $timthumb_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'full');
                $post_timthumb = '<img src="'.get_bloginfo("template_url").'/timthumb.php?src='.$timthumb_src[0].'&amp;h=78&amp;w=78&amp;zc=1" alt="'.$post_title.'" />';
                $output .= '<li><a class="pic" href="'.get_permalink().'" title="'.$post_title.'">'.$post_timthumb.'</a></li>';
            } else {
                $post_timthumb = '';
                ob_start();
                ob_end_clean();
                $temp = preg_match('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $index_matches);//获取日志中第一张图片
                $first_img_src = $index_matches [1];    //获取该图片 src
                if( !empty($first_img_src) ){    //如果日志中有图片
                    $post_timthumb = '<img src="'.get_bloginfo("template_url").'/timthumb.php?src='.$first_img_src.'&amp;h=78&amp;w=78&amp;zc=1" alt="'.$post_title.'" />';
                    $output .= '<li><a class="pic" href="'.get_permalink().'" title="'.$post_title.'">'.$post_timthumb.'</a></li>';
                } else {    //如果日志中没有图片,则显示默认
                    $post_timthumb = '<img src="'.get_bloginfo("template_directory").'/img/default.gif" />';
                    $output .= '<li><a class="pic" href="'.get_permalink().'" title="'.$post_title.'">'.$post_timthumb.'</a></li>';
                }
            }
        }
    } else {
        $output = '<li>N/A</li>'."\n";
    }
    if($display) {
        echo $output;
    } else {
        return $output;
    }
}

然后,在需要的地方可以如下调用:

<?php
    echo '<ul>';
    most_viewed('post',10,true);
    echo '</ul>';
?>

好了,代码很简单直接诶调用就好了,你也可以根据需要改成年度排行,周排行,日排行等等。

© 2022 多姿
Theme by Wing