Template Tags/query posts

来自站长百科
Fludlen讨论 | 贡献2008年7月24日 (四) 16:30的版本
跳转至: 导航、​ 搜索

Description

描述

Query_posts can be used to control which posts show up in WordPress:The Loop. It accepts a variety of parameters in the same format as used in your URL. (e.g. p=4 to show only post of ID number 4)

Query_posts可以用来控制在The Loop上显示哪篇文章,接受你的URL上使用的同样格式的不同的参数。(例如p=4值显示ID为4的文章)

Why go through all the trouble of changing the query that was meticulously created from your given URL? You can customize the presentation of your blog entries by combining it with page logic (like the WordPress:Conditional Tags) -- all without changing any of the URLs.

为什么要经过这麽多的麻烦,更改为给定的URL上创建的查询?你可以自定义博客文章的外观,将外观与网页logic联合起来(像条件式标签—没有更改任何的URLs。

Common uses might be to:

一般的使用情况可能是:

  • Display only a single post or page on your homepage.
  • 只显示一篇文章或者你的主页。
  • Show all posts from a particular time period.
  • 显示一个特殊时间段的所有文章。
  • Show the latest post (only) on the front page.
  • 在首页上(只)显示最近的文章。
  • Change how posts are ordered.
  • 更改文章的排列次序。
  • Show posts from only one category.
  • 只显示来自一个类别中的文章。

Usage

用法

Place a call to query_posts() in one of your Template files before WordPress:The Loop begins. The wp_query object will generate a new SQL query using your parameters. When you do this, WordPress ignores the other parameters it receives via the URL (such as page number or category). If you want to preserve that information, you can use the variable $query_string in the call to query_posts().

The Loop开始之前,在你的一个模板文件上的query_posts()上放置一个call。wp_query会产生一个新的使用你的参数的SQL查询。当你执行这一步的时候,WordPress忽视了通过URL接收到的其它的参数(例如网页号或者类别)。如果你想保存这些信息,你可以在调用中使用变数$query_stringquery_posts()

For example, to set the display order of the posts without affecting the rest of the query string, you could place the following before WordPress:The Loop:

例如,设置文章显示的顺序,但是不干扰其余的查询字符串,你可以将下面的内容放在The Loop之前:

query_posts($query_string . "&order=ASC")

query_posts($query_string . "&order=ASC")

When using query_posts in this way, the quoted portion of the argument must begin with an ampersand (&).

以这种方式使用query_posts的时候,参数的引用部分必须以一个&号开始。

Examples

例子

Exclude Categories From Your Home Page

从你的主页上删除类别

Placing this code in your index.php file will cause your home page to display posts from all categories except category ID 3.

将这个代码放到你的index.php文件上会导致你的主页显示所有类别的文章但是类别ID3的文章除外。

<?php
   if (is_home()) {
      query_posts("cat=-3");
   }
?>
<?php
   if (is_home()) {
      query_posts("cat=-3");
   }
?>

You can also add some more categories to the exclude-list(tested with WP 2.1.2): 你可以在已经删除的类别的列表上在添加几个类别(在WP2.1.2上测试了):

<?php
   if (is_home()) {
      query_posts("cat=-1,-2,-3");
   }
?>


<?php
   if (is_home()) {
      query_posts("cat=-1,-2,-3");
   }
?>


Retrieve a Particular Post

重新得到一篇特别的文章

To retrieve a particular post, you could use the following:

想要重新得到一篇特别的文章,你可以使用以下:

<?php
// retrieve one post with an ID of 5
query_posts('p=5');      
?>


<?php
// 重新得到ID为5的文章
query_posts('p=5');      
?>

If you want to use the Read More functionality with this query, you will need to set the global $more variable to 0.

如果你想对这个查询使用阅读更多的功能,你需要将全局$更多的变数设置为0。

<?php
// retrieve one post with an ID of 5
query_posts('p=5');
      
global $more;
// set $more to 0 in order to only get the first part of the post
$more = 0; 

// the Loop
while (have_posts()) : the_post(); 
  // the content of the post
  the_content('Read the full post »'); 
endwhile;
?>



<?php
// 返回一篇ID为5的文章
query_posts('p=5');
      
global $more;
//将 $更多的设置为0以得到文章的第一部分 
$more = 0; 

// the Loop
while (have_posts()) : the_post(); 
  //文章的内容
  the_content('阅读整篇文章 »'); 
endwhile;
?>

Retrieve a Particular Page

返回一个特别的网页

To retrieve a particular page, you could use the following: 你可以使用以下的内容,返回一个特别的网页:

<?php
query_posts('page_id=7');      //retrieves page 7 only
?>

or

<?php
query_posts('pagename=about'); //retrieves the about page only
?>


<?php
query_posts('page_id=7');      //只返回网页7
?>

or

<?php
query_posts('pagename=about'); //只返回关于网页
?>

For child pages, the slug of the parent and the child is required, separated by a slash. For example:

对于子网页,需要母网页和子网页的slug,用一个斜线分开。例如:

<?php
query_posts('pagename=parent/child'); // retrieve the child page of a parent
?>


<?php
query_posts('pagename=parent/child'); // 返回母网页的子网页
?>

Passing variables to query_posts

将变数传给query_posts

You can pass a variable to the query with two methods, depending on your needs. As with other examples, place these above your Loop:

根据你的需要,你可以使用两种方法将一个变数传给查询。如其它的例子那样,将这些放置到你的Loop的上面:

Example 1

例 1

In this example, we concatenate the query before running it. First assign the variable, then concatenate and then run it. Here we're pulling in a category variable from elsewhere.

在这个例子中,我们连接查询之后,再运行查询。首先分配了变数,然后再连接,然后再运行。在这个例子中,我们其它地方引入了一个类别变数。

 <?php
 $categoryvariable=$cat; // assign the variable as current category
 $query= 'cat=' . $categoryvariable. '&orderby=date&order=ASC'; // concatenate the query
 query_posts($query); // run the query
 ?>


 <?php
 $categoryvariable=$cat; // 指派变数作为当前的类别
 $query= 'cat=' . $categoryvariable. '&orderby=date&order=ASC'; // concatenate the query
 query_posts($query); // 运行查询
 ?>

Example 2

例 2

In this next example, the double " quotes " tell PHP to treat the enclosed as an expression. For this example, we are getting the current month and the current year, and telling query posts to bring us the posts for the current month/year, and in this case, listing in ascending so we get oldest post at top of page.

在下面的这个例子中,双" 引号 "指示PHP将enclosed看做一个表述。在这个例子中,我们得到当前的年份和月份,而且通知query文章给我们提供当前年/月的文章,而且在这种情况下,将这些文章按升序排列,这样我们可以在网页的顶上方,得到最早所写的文章。


<?php $current_month = date('m'); ?>
<?php $current_year = date('Y'); ?>

<?php query_posts("cat=22&year=$current_year&monthnum=$current_month&order=ASC"); ?>
<!-- put your loop here -->




<?php $current_month = date('m'); ?>
<?php $current_year = date('Y'); ?>

<?php query_posts("cat=22&year=$current_year&monthnum=$current_month&order=ASC"); ?>
<!—将你的loop放在这儿 -->

Example 3

例 3

This example explains how to generate a complete list of posts, dealing with pagination. We can use the default $query_string telling query posts to bring us a full posts listing. We can also modify the posts_per_page query argument from -1 to the number of posts you want to show on each page; in this last case, you'll probably want to use posts_nav_link() to navigate the generated archive.

这个例子演示了怎样创建所有的文章的列表,并且标记页数。我们可以使用默认的$查询_字符串,这个字符串通知查询文章给我们提供全部文章的列表。我们也可以将每_页_文章查询参数从-1改为我们希望在每个网页上显示的文章的数目;在最后一种情况中,你可能想要使用 posts_nav_link()导航到创建的归档。

.

<?php 
query_posts($query_string.'posts_per_page=-1');
while(have_posts()) { the_post();
<!-- put your loop here -->
}
?>



.

<?php 
query_posts($query_string.'posts_per_page=-1');
while(have_posts()) { the_post();
<!-- put your loop here -->
}
?>


Parameters

参数

This is not an exhaustive list yet. It is meant to show some of the more common things possible with setting your own queries.

这还不是一个详尽的列表,意思是设置你自己的queries,可以显示一些更常见的内容。

Category Parameters

类别 参数

Show posts only belonging to certain categories.

显示属于某个类别的文章。

  • cat
  • category_name
  • cat
  • 类别_名称

Show One Category by ID

根据ID显示一个类别

Display posts from only one category ID:

只显示来自一个类别ID的文章:

query_posts('cat=4');

query_posts('cat=4');

Show One Category by Name

根据名称显示一个类别

Display posts from only one category by name:

只显示属于某个类别名的文章:

query_posts('category_name=Staff Home');

query_posts('category_name=Staff Home');

Show Several Categories by ID

显示几个类别及ID

Display posts from several specific category IDs:

显示属于几个类别ID的文章:

query_posts('cat=2,6,17,38');

query_posts('cat=2,6,17,38');

Exclude Posts Belonging to Only One Category

删除某个类别的文章

Show all posts except those from a category by prefixing its ID with a '-' (minus) sign.

显示所有的文章,但是类别ID前面有个'-'(负号)负号的类被除外。

query_posts('cat=-3');

query_posts('cat=-3');

This excludes all the posts that belong only to category 3. There is a proviso however: it will exclude all the posts that belong only to category 3. If a post belongs to another category as well, it will still be picked up.

删除属于类别3的所有文章。有一个限制性条款:会删除属于类别3的所有文章。如果一个类别也同时属于其它的类别,这个类别仍然不会被删除。

Tag Parameters

标签参数

Show posts associated with certain tags.

  • tag

显示与某个标签相关的文章。

  • 标签

Fetch posts for one tag

query_posts('tag=cooking');

为某个标签提取文章

query_posts('tag=cooking');

Fetch posts that have either of these tags

获得拥有任何这样的标签的文章

query_posts('tag=bread,baking');

query_posts('tag=bread,baking');

Fetch posts that have all three of these tags:

获取拥有这三个标签的文章:

query_posts('tag=bread+baking+recipe');

query_posts('tag=bread+baking+recipe');

Also see Ryan's discussion of Tag intersections and unions.

也看看Ryan's discussion of Tag intersections and unions

Author Parameters

作者参数

You can also restrict the posts by author.

你也可以根据作者限制文章数目。

  • author_name=Harriet
  • author=3
  • author_name=Harriet
  • 作者=3

author_name operates on the user_nicename field, whilst author operates on the author id.

author_nameuser_nicename区操作, 同时作者 在作者id上操作。

Post & Page Parameters

文章 & 网页参数

Retrieve a single post or page.

返回一篇单独的文章或者一个单独的网页。

  • p=1 - use the post ID to show the first post
  • name=first-post - use the Post Slug to show the first post
  • page_id=7
  • pagename=about
  • showposts=1 (you can also use showposts=3, or any number to display a limited number of posts


  • p=1 - 使用文章 ID来显示第一篇文章
  • 名称=第一篇文章 - 使用 文章 Slug 显示第一篇文章
  • 网页_id=7
  • 网页名=关于
  • 显示文章=1 (你可以使用 显示文章=3,或者其它的任何数字显示一定数目的文章

As a consequence of the WordPress:Template Hierarchy, home.php executes first. This means that you can write a home.php which calls query_posts() to retrieve a particular page and set that to be your front page. Without any plugins or hacks, you have got a mechanism to run, show and maintain a non-bloggy front page.

由于 模板层级方面的原因, home.php先执行了。这意味这你可以编写一个home.php,home.phh调用query_posts()重新得到一个特别的网页并且将那个网页设置为你的首页。没有任何插件或者hacks,你需要运行一个机制,并且显示和维护一个非博客的首页。

More useful perhaps would be to take advantage of WP’s Page functionality and use that for your front page. You could set your "about page" to be the entry point or maybe your site's colophon. You might even do something a bit more dynamic and set a custom page that shows a list of the latest comments, posts, categories and archives. See the example below.

更有用的方法,可能是利用WP的网页功能并且为你的首页使用这个功能。你可以将"关于网页"设置为entry point或者设置为站点的末页。你可能执行一些更动态的步骤,设置一个自定义网页,显示最近的评论,文章,类别,存档。请看看下面的例子。

Time Parameters

时间参数

Retrieve posts belonging to a certain time period.

得到某个特别的时间段内发表的文章。

  • hour=
  • minute=
  • second=
  • day= - day of the month; shows all posts made, for example on the 15th.
  • monthnum=
  • year=
  • 小时=
  • 分钟=
  • 秒=
  • 日= - 一个月中的每一天; 显示,例如,十五号发表的所有文章。
  • monthnum=
  • 年=

Page Parameters

网页参数

  • paged=2 - show the posts that would normally show up just on page 2 when using the "Older Entries" link.
  • paged=2 -显示使用"以前发表的文章"链接时,通常在网页2上显示的文章。
  • posts_per_page=10 - number of posts to show per page; a value of -1 will show all posts.
  • posts_per_page=10 -每个网页显示的文章数目;-1这个值,会显示所有的文章。
  • order=ASC - show posts in chronological order, DESC to show in reverse order (the default)
  • 顺序=ASC -按时间顺序显示文章,以相反的顺序显示DESC(默认)

Offset Parameter

Offset 参数

You can displace or pass over one or more initial posts which would normally be collected by your query through the use of the offset parameter.

你不能转移或者忽视一个或者更多的原始文章,这些文章一般是你的query同时使用offset参数收集到的。

The following will display the 5 posts which follow the most recent (1):

下面的函数会显示(1)最近的5篇文章:

query_posts('showposts=5&offset=1');

query_posts('showposts=5&offset=1');

Orderby Parameters

根据参数排序

Sort retrieved posts by this field.

根据这个区给得到的文章排序。

  • orderby=author
  • orderby=date
  • orderby=category
  • orderby=title
  • orderby=modified
  • orderby=modified
  • orderby=menu_order
  • orderby=parent
  • orderby=ID
  • orderby=rand
  • 排序根据=作者
  • 排序根据=日期
  • 排序根据=类别
  • 排序根据=标题
  • 排序根据=更改的
  • 排序根据=更改的
  • 排序根据=menu_顺序
  • 排序根据=parent
  • 排序根据=ID
  • 排序根据=rand

Also consider order parameter of "ASC" or "DESC"

同时考虑"ASC"或者的"DESC"的排序参数

Combining Parameters

联合参数

You may have noticed from some of the examples above that you combine parameters with an ampersand (&), like so:

你可能从上面的例子中注意到,你使用一个&(&符号)将参数组合在一起,像:

query_posts('cat=3&year=2004');

query_posts ('cat=3&年份=2004');

Posts for category 13, for the current month on the main page:

类别13,关于当前月份显示在主页上的文章:

if (is_home()) {
query_posts($query_string . '&cat=13&monthnum=' . date('n',current_time('timestamp')));
}

if (is_home()) {

query_posts ($query_string . '&cat=13&monthnum=' . date('n',current_time('timestamp')));
}

At 2.3 this combination will return posts belong to both Category 1 AND 3, showing just two (2) posts, in descending order by the title:

在2.3版本中,这个参数组合会返回属于类别1同时属于类别3的文章,只显示两篇(2)文章,根据标题,按降序排列:

 query_posts(array('category__and'=>array(1,3),'showposts'=>2,'orderby'=>title,'order'=>DESC));


query_posts (array('category__and'=>array(1,3),'显示文章'=>2,'orderby'=>标题,'顺序'=>DESC));


In 2.3 and 2.5 one would expect the following to return all posts that belong to category 1 and is tagged "apples"

在2.3和2.5版本中,你可能期待下面的内容,返回属于类别1并且标签为"苹果"的所有文章

query_posts('cat=1&tag=apples');

query_posts ('cat=1&标签=苹果');

A bug prevents this from happening. See Ticket #5433. A workaround is to search for several tags using +

一个bug阻止这个运行。请看看Ticket #5433,一个工作区要搜索几个使用+的标签

query_posts('cat=1&tag=apples+apples');

query_posts ('cat=1&tag=苹果+苹果');

This will yield the expected results of the previous query. Note that using 'cat=1&tag=apples+oranges' yields expected results.

对于先前的查询,这个会产生期待的结果。注意使用'cat=1&tag=苹果+橘子'能够产生期待的结果。

Resources

资源



<!—我们需要关于这篇文章的其它资源-->

Related

模板:Tag General Tags

模板:Tag Footer

相关的

模板:标签总标签