如题,想写一个导航栏,导航栏的每个分类拥有一个气泡显示一周内更新的文章数量。
已知获取一个分类的全部文章数量是
<code><?php $posts = get_posts( 'numberposts=-1&category=分类id1,分类id2,分类id3' ); echo count($posts); ?> </code>
请问如何实现显示一周内发布文章的数量?谢谢..
大致效果类似于b站这样
回复内容:
如题,想写一个导航栏,导航栏的每个分类拥有一个气泡显示一周内更新的文章数量。
已知获取一个分类的全部文章数量是
<code><?php $posts = get_posts( 'numberposts=-1&category=分类id1,分类id2,分类id3' ); echo count($posts); ?> </code>
请问如何实现显示一周内发布文章的数量?谢谢..
大致效果类似于b站这样
好久没看到worpdress的问题了.
本人正好非常熟悉wordpress开发.
这是我写的函数.
<br>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 中
<code>function newArticle($num,$cat){ $args=array( 'posts_per_page' => $num, 'cat' => $cat, 'order' => 'desc' ); $posts = query_posts($args); if( have_posts() ) : $html = '<div class="newArticle">'; $html .= '<ul>'; foreach($posts as $post) : $html .= '<li>'; $html .= '<a href="'.get_permalink(%24post->ID).'" rel="bookmark" title="'.$post->post_title.'">'.$post->post_title.'</a>'; $html .= '</li>'; endforeach; $html .= '</ul>'; $html .= '</div>'; endif; echo $html; } </code>
调用指定分类下指定数量的文章,分类ID编号可以在wordpress后台看到
<code>newArticle($num,$cat); // $num 要显示的数量; $cat 制定分类的ID </code>
指定数量的最新文章:
<code>newArticle($num,null); // $num 要显示的数量; 注意:null 一定要填写,否则出现PHP报错 </code>
调用完成以后。你可以自己写一段代码,思路差不多是:需要设定一个制定时间,比如七天内这个分类出现的最新文章。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); } }
调用
<?php echo wt_get_category_count($id);?>
变量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计数。

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
