WordPress提供了WP_Query类解决类似问题,下边是官方的介绍地址:http://codex.wordpress.org/Function_Reference/WP_Query。
简单应用举例:
1 |
|
2 |
$recentPosts = new WP_Query(); //实例化 |
3 |
$recentPosts ->query( 'cat=7&showposts=2' ); //返回分类ID为7的2篇内容 |
4 |
?> |
5 |
|
7 |
|
这个类有不少属性和方法,恩,无责任翻译下吧(有兴趣的最好直接看官方E文版):
Properties属性
$query
Holds the query string that was passed to the $wp_query object by wp-blog-header.php (or the WP class in Version 2.0). For information on the construction of this query string (in WP1.5 with wp-blog-header.php – the following link is fairly outdated now), see WordPress Code Flow.
传给$wp_query的查询字符串
$query_vars
An associative array containing the dissected $query: an array of the query variables and their respective values.
保存多个查询参数的关联数组
$queried_object
Applicable if the request is a category, author, permalink or Page. Holds information on the requested category, author, post or Page.
如果request里的是分类,作者,永久链接或者页面,那么返回的结果集将储存在分类,作者,永久链接或者页面里。
$queried_object_id
Simply holds the ID of the above property.
只返回$queried_object结果集中的ID
$posts
Gets filled with the requested posts from the database.
获得符合要求的posts,也就是只返回日志。
$post_count
The number of posts being displayed.
日志数
$current_post
(available during The Loop) Index of the post currently being displayed.
当前显示的日志的索引
$post
(available during The Loop) The post currently being displayed.
当前显示的日志
$is_single, $is_page, $is_archive, $is_preview, $is_date, $is_year, $is_month, $is_time, $is_author, $is_category, $is_tag, $is_tax, $is_search, $is_feed, $is_comment_feed, $is_trackback, $is_home, $is_404, $is_comments_popup, $is_admin, $is_attachment, $is_singular, $is_robots, $is_posts_page, $is_paged
Booleans dictating what type of request this is. For example, the first three represent ‘is it a permalink?’, ‘is it a Page?’, ‘is it any type of archive page?’, respectively.
判断是否是single页啊,page页啊之类的
———————————————————–吾乃华丽的分割线———————————————————–
接着是方法Methods:
init()
Initialise the object, set all properties to null, zero or false.
初始化,将所有属性设置为null,0,或者false。
parse_query($query)
Takes a query string defining the request, parses it and populates all properties apart from $posts, $post_count, $post and $current_post.
设置一个查询字符串,解析它,并且配置除了$posts, $post_count, $post and $current_post的属性。
parse_query_vars()
Reparse the old query string.
重新解析旧的查询字符串。
get($query_var)
Get a named query variable.
获得某一个命名的查询变量。
set($query_var, $value)
Set a named query variable to a specific value.
设置一个查询变量的值
&get_posts()
Fetch and return the requested posts from the database. Also populate $posts and $post_count.
返回符合查询的posts,包括$posts and $post_count.
next_post()
(to be used when in The Loop) Advance onto the next post in $posts. Increment $current_post and set $post.
在循环中使用,在$posts中向前增加$current_post and set $post.
the_post()
(to be used when in The Loop) Advance onto the next post, and set the global $post variable.
同上,不过这个是这是全局变量$post
have_posts()
(to be used when in The Loop, or just before The Loop) Determine if we have posts remaining to be displayed.
可在循环体内外使用,决定是否还有需要显示的posts。
rewind_posts()
Reset $current_post and $post.
回复$current_post and $post的默认值。
&query($query)
Call parse_query() and get_posts(). Return the results of get_posts().
调用parse_query() and get_posts(),返回get_posts()的结果。
get_queried_object()
Set $queried_object if it’s not already set and return it.
如果$queried_object未设置的话,设置$queried_object的值,并且返回这个结果。
get_queried_object_id()
Set $queried_object_id if it’s not already set and return it.
如果$queried_object_id 未设置的话,设置$queried_object_id 的值,并且返回这个结果。
WP_Query($query = ”) (constructor)
If you provide a query string, call query() with it.
这个是构造函数,变量值是查询字符串。
转载请注明出处:http://www.onexin.net/wp_query-function-in-the-use-of-wordpress/