WordPress如何提取某一时间段浏览量最多的文章

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

默认文章类型

<?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
$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' => 18,
'post_type' => '文章类型',
'taxonomy' => '分类法',
'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();?>
<li> <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" target="_blank">"
<?php the_title(); ?>
 </a> </li>
<?php endwhile;wp_reset_query();?>

给TA打赏
共{{data.count}}人
人已打赏
WP笔记

WordPress4.9火车头免登陆发布接口+模块(增强版)

2023-2-22 11:09:24

WP笔记

wordpress自动保存远程图片代码

2023-3-23 8:51:41

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索