使用Wordpress做网站,有时需要在网站的侧边栏调用热门文章,我们经常是按评论数进行排序的,今天介绍一下如何调用以浏览量排序的热门文章的方法。(相关教程:wordpress如何调用某一个分类下的热门文章(热评文章))
方法/步骤
先要在自己的WORDPRESS网站模板函数文件functions.php中添加以下的浏览量函数代码,用于调用文章浏览量。代码见:wordpress免插件显示文章浏览量次数; 然后在需要调用按浏览量排序的热门文章位置,使用以下的代码进行调用文章列表; <ul>
<?php $args=array(
meta_key => views,
orderby => meta_value_num,
posts_per_page=>6,
order => DESC
);
query_posts($args); while (have_posts()) : the_post();?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><span class="kc-view fright">浏览:<?phpsetPostViews(get_the_ID()); echo number_format(getPostViews(get_the_ID())); ?></span></li>
<?php endwhile;wp_reset_query();?>
</ul>
更新:如果想对显示的文章列表做控制,可以使用以下的代码:
1、排除置顶文章
<ul>
<?php $args=array(
meta_key => post_views_count,
orderby => meta_value_num,
post__not_in => get_option( sticky_posts ),
posts_per_page=>6,
order => DESC
);
query_posts($args); while (have_posts()) : the_post();?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><span class="kc-view fright">浏览:<?php setPostViews(get_the_ID()); echo number_format(getPostViews(get_the_ID())); ?></span></li>
<?php endwhile;wp_reset_query();?>
</ul>
2、排除指定分类下的文章
<ul>
<?php $args=array(
meta_key => post_views_count,
orderby => meta_value_num,
post__not_in => get_option( sticky_posts ),
category__not_in => array(1,2),
posts_per_page=>6,
order => DESC
);
query_posts($args); while (have_posts()) : the_post();?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><span class="kc-view fright">浏览:<?php setPostViews(get_the_ID()); echo number_format(getPostViews(get_the_ID())); ?></span></li>
<?php endwhile;wp_reset_query();?>
</ul>
3、调用指定分类下的热门文章
<ul>
<?php $args=array(
meta_key => post_views_count,
orderby => meta_value_num,
post__not_in => get_option( sticky_posts ),
cat => array(1,2),
posts_per_page=>6,
order => DESC
);
query_posts($args); while (have_posts()) : the_post();?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><span class="kc-view fright">浏览:<?php setPostViews(get_the_ID()); echo number_format(getPostViews(get_the_ID())); ?></span></li>
<?php endwhile;wp_reset_query();?>
</ul>
4、提取30天时间段浏览量最多的文章。
<?php
$date_query=array(
array(
column => post_date,
before => date(Y-m-d,time()+3600*24),
after =>date(Y-m-d,time()-3600*24*30) //此处30标示三十天内浏览量
)
);
?>
<?php
$args = array(
posts_per_page =>1,
cat =>26,
post__in => get_option( sticky_posts ),
ignore_sticky_posts => 1,
meta_key => views,
date_query => $date_query,
orderby=> meta_value_num,
);
query_posts( $args ); while ( have_posts() ) : the_post();?>
<h4><b><a href="<?php the_permalink(); ?>"><?php echo mb_strimwidth(get_the_title(), 0,100, ); ?></a>
</b></h4>
<?php endwhile;wp_reset_query();?>
标签: