欢迎来到站长教程网!

PHP教程

当前位置: 主页 > 网络编程 > PHP教程

WordPress网站制作热门文章排行榜(浏览量排序)

更新时间:2025-09-13 12:15:34|栏目:PHP教程|点击:19 次

使用Wordpress做网站,有时需要在网站的侧边栏调用热门文章,我们经常是按评论数进行排序的,今天介绍一下如何调用以浏览量排序的热门文章的方法。

 

方法/步骤:

1、先要在自己的WORDPRESS网站模板函数文件functions.php中添加以下的浏览量函数代码,用于调用文章浏览量。代码见:

wordpress显示文章浏览量次数[文章]

 

2、然后在需要调用按浏览量排序的热门文章位置,使用以下的代码进行调用文章列表;

<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">浏览:<?php setPostViews(get_the_ID()); echo number_format(getPostViews(get_the_ID())); ?></span></li>
<?php endwhile;wp_reset_query();?>
</ul>

3、这样就可以调用出用户浏览最多的6篇文章了。

 

 

更新:如果想对显示的文章列表做控制,可以使用以下的代码:

 

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();?>

上一篇:PHP中实现一个简易三角形的方法

栏    目:PHP教程

下一篇:PHP 7安装使用体验之性能大提升,兼容性强,扩展支

本文标题:PHP中实现九九乘法口诀表

本文地址:http://tp2.com/index.php?m=home&c=View&a=index&aid=172

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:88888888 | 邮箱:888petersds@gmail.com

Copyright © 2002-202X 某某站长教程网 版权所有 非商用版本粤ICP备xxxxxxxx号