系统管理 维护监控 简单生活
wordpress
Wordpress插件WP-PostViews的调用方法
八 26th
l转载:http://haola.cn/archives/2013.html
一、插件介绍
该插件实现的功能是统计某篇文章被阅读次数并且可以实现侧边栏 Widget 以及显示最受欢迎(阅读数最多)文章、某分类下最受欢迎(阅读数最多)文章等功能,支持最新的 WP 2.3 版本。
二、安装方法
- 将解压得到的 postviews 文件夹上传到 /wp-content/plugins 文件夹中
- 激活 WP-PostViews 插件,然后可以去 Options 中设置
三、使用方法
(一)一般应用
- 修改 /wp-content/themes/<YOUR THEME NAME>/index.php(single.php, post.php 或 page.php)
- 如果修改 index.php 找到PHP代码
<?php while (have_posts()) : the_post(); ?>
- 将下面的语句添加到如何需要的地方PHP代码
<?php if(function_exists(’the_views’)) { the_views(); } ?>
- 登录“WP-Admin -> Options -> Post Views”可以配置该插件
Count Views From 设置被统计的用户群
Views Template 该插件显示样式
(二)侧边栏 Widget 使用
- 激活 “WP-PostViews Widget” 插件
- 登录 “’WP-Admin -> Presentation -> Widgets”
- 为了显示“Most Viewed Post(最受欢迎文章)”拖曳(Drag)“Most Viewed Widget”到侧边栏
- 可以点击配置图标对 “Most Viewed Widget” 进行配置,然后点击“Save changes”保存
(三)调用使用
1、显示最受欢迎(阅读次数最多)文章,使用下面语句
PHP代码
<?php if (function_exists(’get_most_viewed’)): ?>
<?php get_most_viewed(); ?>
<?php endif; ?>
第一个值是你想得到的列表的类型是“文章”或“页面”或“两者都有”,如“’pos”、“page”或者“both”
第二个值是你想得到的列表的对象个数
默认的是这样的:
PHP代码
get_most_viewed(’both’, 10); //获取被阅读次数最多的10篇文章和页面
2、显示某个分类下最受欢迎(阅读次数最多)文章
PHP代码
<?php if (function_exists(’get_most_viewed_category’)): ?>
<?php get_most_viewed_category(); ?>
<?php endif; ?>
第一个值是分类目录的ID
第二个值是列表的类型“文章”或“页面”或“两者都有”,如“post”、“page”、“both’”
第三个值是列表中对象的个数
默认是这样的:
PHP代码
get_most_viewed_category(1, ‘both’, 10); //ID为1的目录下阅读最多的10篇文章和页面
四、升级方法
- 禁用 WP-PostViews 插件
- 上传并覆盖 /wp-content/plugins 中的 postviews 文件夹
- 删除 postviews.php 文件
- 激活 WP-PostViews 插件
How to display popular posts without plugin?
八 25th
转载:http://iwp.me/how-to-display-popular-posts-without-plugin/
php 代码:
< ?php
function popular_posts($no_posts = 5, $before = '', $after = '', $show_pass_post = false, $duration='')
{
global $wpdb;
$request = "SELECT ID, post_title, COUNT($wpdb->comments.comment_post_ID) AS 'comment_count' FROM $wpdb->posts, $wpdb->comments";
$request .= " WHERE comment_approved = '1' AND $wpdb->posts.ID=$wpdb->comments.comment_post_ID AND post_status = 'publish'";
if(!$show_pass_post) $request .= " AND post_password =''";
if($duration !="") { $request .= " AND DATE_SUB(CURDATE(),INTERVAL ".$duration." DAY) < post_date "; } $request .= " GROUP BY $wpdb->comments.comment_post_ID ORDER BY comment_count DESC LIMIT $no_posts";
$posts = $wpdb->get_results($request);
$output = '';
if ($posts) {
foreach ($posts as $post) {
$post_title = stripslashes($post->post_title);
$comment_count = $post->comment_count;
$permalink = get_permalink($post->ID);
$output .= $before . '' . $post_title . ' (' . $comment_count.')' . $after;
}
} else {
$output .= $before . "None found" . $after;
}
echo $output;
} ?>
< ?php most_popular_posts(); ?>
最新评论