本文适合安装了wp-postviews这个插件或者非插件实现文章浏览数的同学,如果你都没有使用可以参考我的非插件实现文章浏览数,现在浏览量基本算是WP的标配了吧。
无论是插件还是网上的教程获取浏览数最多的文章都是使用sql语句来实现的,非插件其实也是从插件提取出来的,囧。
直接使用官方推荐的get_posts
,省时省力,把下面的代码放到你想调用的地方,如果你想调整样式加上一些标签然后自己写点css就好了。
<ul> <?php $args = array( 'posts_per_page' => 5,//文章数 'meta_key' => 'views', 'orderby' => 'meta_value_num', 'date_query' => array( array( 'after' => '2 month ago',//时间范围 )) ); $postslist = get_posts( $args ); foreach ( $postslist as $post ) : setup_postdata( $post ); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><span> - <?php if ( function_exists('custom_the_views') ) custom_the_views($post->ID);//如果安装的是插件就换成插件的调用方法 ?></span></li> <?php endforeach; wp_reset_postdata(); ?> </ul>