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

[ wordpress教程   // 2014-08-23  ]
64

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

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

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

<ul>
<?php $post_query = new WP_Query('showposts=10′);
while ($post_query->have_posts()) : $post_query->the_post();$do_not_duplicate = $post->ID; ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><href="http://www.softhome.cc/li>
<?php endwhile;?>
</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 = new WP_Query();$query_posts->query($args);
while( $query_posts->have_posts() ) { $query_posts->the_post(); ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a><href="http://www.softhome.cc/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 = new WP_Query();$query_posts->query($args);?>
<?php while ($query_posts->have_posts()) : $query_posts->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>" rel="bookmark" title="<?phpthe_title_attribute(); ?>">
<?php the_title(); ?></a><href="http://www.softhome.cc/li><?php endwhile; ?></ul>

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

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

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

<?php get_most_viewed('post', 15, 150, true, false); ?><?php if (function_exists('get_most_viewed')): ?><?php endif; ?>