您好,欢迎访问一九零五行业门户网

wordpress如何显示指定分类一周内最新文章数量

如题,想写一个导航栏,导航栏的每个分类拥有一个气泡显示一周内更新的文章数量。
已知获取一个分类的全部文章数量是


请问如何实现显示一周内发布文章的数量?谢谢..
大致效果类似于b站这样
回复内容: 如题,想写一个导航栏,导航栏的每个分类拥有一个气泡显示一周内更新的文章数量。
已知获取一个分类的全部文章数量是


请问如何实现显示一周内发布文章的数量?谢谢..
大致效果类似于b站这样
好久没看到worpdress的问题了.
本人正好非常熟悉wordpress开发.
这是我写的函数.
function get_this_week_post_count_by_category($id){ $date_query = array( array( 'after'=>'1 week ago' ) ); $tax_query = array( array( 'taxonomy' => 'category', 'field' => 'id', 'terms' => $id ) ); $args = array( 'post_type' => 'post', 'post_status'=>'publish', 'tax_query' => $tax_query, 'date_query' => $date_query, 'no_found_rows' => true, 'suppress_filters' => true, 'fields'=>'ids', 'posts_per_page'=>-1 ); $query = new wp_query( $args ); return $query->post_count;}
由于使用到date_query, 所以这个只适用3.7+
--
update:
给wp_query添加一些参数进行优化.
生成的sql,大概是这样的
select wp_posts.id from wp_posts inner join wp_term_relationships on (wp_posts.id = wp_term_relationships.object_id) where 1=1 and ( ( post_date > '2014-02-04 10:47:10' ) ) and ( wp_term_relationships.term_taxonomy_id in (247) ) and wp_posts.post_type = 'post' and (wp_posts.post_status = 'publish') group by wp_posts.id order by wp_posts.post_date desc
如果数据比较大,建议使用上面sql,搭配$wpdb->get_var($sql).
记住要使用count(wp_posts.id), 毕竟聚合函数开销小点
另外给你一个思路。
放到 functions.php 中
function newarticle($num,$cat){ $args=array( 'posts_per_page' => $num, 'cat' => $cat, 'order' => 'desc' ); $posts = query_posts($args); if( have_posts() ) : $html = ''; $html .= ''; foreach($posts as $post) : $html .= ''; $html .= 'id).' rel=bookmark title='.$post->post_title.'>'.$post->post_title.''; $html .= ''; endforeach; $html .= ''; $html .= '
'; endif; echo $html;}
调用指定分类下指定数量的文章,分类id编号可以在wordpress后台看到
newarticle($num,$cat); // $num 要显示的数量; $cat 制定分类的id
指定数量的最新文章:
newarticle($num,null); // $num 要显示的数量; 注意:null 一定要填写,否则出现php报错
调用完成以后。你可以自己写一段代码,思路差不多是:需要设定一个制定时间,比如七天内这个分类出现的最新文章。wordpress默认应该是没有这个功能的。你可以在数据库查询,有多少内容,然后缓存到一个sql表单,wordpress前台读取这个表单的数值。
或者说,给每个分类的提示数量设置一个基数(比如0)。然后读取,这个分类下面更新过多少文章(前提做好时间范围控制)然后在这个基数上面添加这个数值。
给你个参考,
function wt_get_category_count($input = '') { global $wpdb; if($input == '') { $category = get_the_category(); return $category[0]->category_count; } elseif(is_numeric($input)) { $sql = select {$wpdb->term_taxonomy}.count from {$wpdb->terms}, {$wpdb->term_taxonomy} where {$wpdb->terms}.term_id = {$wpdb->term_taxonomy}.term_id and {$wpdb->term_taxonomy}.term_id = {$input}; return $wpdb->get_var($sql); } else { $sql = select {$wpdb->term_taxonomy}.count from {$wpdb->terms}, {$wpdb->term_taxonomy} where {$wpdb->terms}.term_id = {$wpdb->term_taxonomy}.term_id and {$wpdb->terms}.slug='{$input}'; return $wpdb->get_var($sql); }}
调用

变量id为分类的id
一个残酷的事实就是:分类(category, 数据表中被归类为term的一种)是没有meta辅助数据的。
所以你不能像记录post meta一样,简单的把这个数目用一个函数记录在分类里,再用另一个函数调出来。这个做不到的。
可行的办法是:先用后台的计划任务,自己用$wpdb数据库操作类,构造sql语句查询出这个个数。然后用wp_options表缓存,每次前台展示时直接调用。
需要的api包含:wordpress cron, wordpress options, class/wpdb。
注:抱歉,其实是没时间写这个sql(逃) 主要的思路是:查询posts + term_relationships两个表(join关系需要考虑一下),筛选某个特定分类(term)下所有的post记录,按post类型(post)、发布状态(publish)和日期(>=特定日期)筛选,最后select count计数。
其它类似信息

推荐信息