我们每天也许每天都在更新文章,但不是所有的文章都被访客所喜欢.
这个时候在你的WordPress主题的侧边栏加一个浏览量多的文章内容,相信很多访客愿意去点击看看;
在这之前你可以试试: WordPress主题开发教程:WP-Postviews调用网站热门文章浏览点击量排名文章列表
在自己使用的wordpress模板函数文件functions.php中加上以下的代码:
/*文章浏览量*/ /*代码来源:WordPress易用教程 https://www.wpyi.com function record_visitors() { if (is_singular()) { global $post; $post_ID = $post->ID; if($post_ID) { $post_views = (int)get_post_meta($post_ID, 'views', true); if(!update_post_meta($post_ID, 'views', ($post_views+1))) { add_post_meta($post_ID, 'views', 1, true); } } } } add_action('wp_head', 'record_visitors'); /// 函数名称:post_views /// 函数作用:取得文章的阅读次数 function post_views($before = '(点击 ', $after = ' 次)', $echo = 1) { global $post; $post_ID = $post->ID; $views = (int)get_post_meta($post_ID, 'views', true); if ($echo) echo $before, number_format($views), $after; else return $views; }
在你的侧边栏想要显示本段文字列表的地方,比如你的WordPress主题的侧边栏,使用以下的代码进行调用:
注意事项:
本段代码需要搭配WP-Postviews插件来使用
<?php $args=array( 'meta_key' => 'views', //自定义字段名称,可能是post_views_count 你要自己看自己的浏览量字段 'orderby' => 'meta_value_num', 'posts_per_page'=>10, 'order' => 'DESC' ); query_posts($args); while (have_posts()) : the_post();?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endwhile;wp_reset_query();?>