当前位置:图集之家知识

wordpress免插件实现调用最新、热门、随机文章的代码

wordpress免插件实现调用最新、热门、随机文章的代码

内容

如果想在wordpress主题中调用最新、热门、随机文章,可以使用wordpress自带的widget侧边栏的小工具功能来实现,但是这样会影响到我们想自定义侧边栏的效果,当然也有一些可以使用插件来实现这个功能的,不过使用插件始终对网站性能方面有一定的影响,下面我给大家介绍一种不用插件也能实现wordpress最新、热门、随机文章的调用方法。

wordpress免插件实现调用最新、热门、随机文章

1、WordPress最新文章的调用代码:

ul?php$post_query=newWP_Query('showposts=10);while($post_query-have_posts()):$post_query-the_post();$do_not_duplicate=$post-ID;?liahref=?phpthe_permalink();??phpthe_title();?/a/li?phpendwhile;?/ul

2、热门文章的调用代码:

ul?php$post_num=10;//设置调用条数$args=array('post_password'=,'post_status'='publish',//只选公开的文章.'post__not_in'=array($post-ID),//排除当前文章'caller_get_posts'=1,//排除置頂文章.'orderby'='comment_count',//依評論數排序.'posts_per_page'=$post_num);$query_posts=newWP_Query();$query_posts-query($args);while($query_posts-have_posts()){$query_posts-the_post();?liahref=?phpthe_permalink();?title=?phpthe_title();??phpthe_title();?/a/li?php}wp_reset_query();?/ul

3、随机文章的调用代码:

ul?phpglobal$post;$postid=$post-ID;$args=array('orderby'='rand','post__not_in'=array($post-ID),'showposts'=10);$query_posts=newWP_Query();$query_posts-query($args);??phpwhile($query_posts-have_posts()):$query_posts-the_post();?liahref=?phpthe_permalink();?rel=bookmarktitle=?phpthe_title_attribute();??phpthe_title();?/a/li?phpendwhile;?/ul

从以上上三种文章的代码调用方法都测试过,其中最新文章和随机文章的调用方法是没有问题的,而热门文章的调用代码不知道为什么不能正常显示,但是删除了post_password = ,这句代码就可以正常调用,不知道有没有那位大神可以留言告诉我原因,还有热门文章的调用,如果想依浏览数来排序又是怎么实现的呢?期高手的出现。

使用WP-PostViews插件实现热门文章代码:

如果想实现文章计数功能,可以安装WP-PostViews插件,使用该插件也可以实现热门文章的功能,利弊自己取舍吧,代码如下:

?phpget_most_viewed('post',15,150,true,false);??phpif(function_exists('get_most_viewed')):??phpendif;?

*特别声明:资源收集自网络或用户上传,本网站所提供的电子文本仅供参考,请以正式出版物为准。电子文本仅供个人标准化学习、研究使用,不得复制、发行、汇编、翻译或网络传播等。如有侵权,请联系我们处理。

相关