Template Tags/query posts

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

描述

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

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

一般的使用情况可能是:

  • 只显示一篇文章或者你的主页。
  • 显示一个特殊时间段的所有文章。
  • 在首页上(只)显示最近的文章。
  • 更改文章的排列次序。
  • 只显示来自一个类别中的文章。

用法

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

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

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

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

例子

从你的主页上删除类别

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

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

你可以在已经删除的类别的列表上在添加几个类别(在WP2.1.2上测试了):


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

重新得到一篇特别的文章

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

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

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

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

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

返回一个特别的网页

你可以使用以下的内容,返回一个特别的网页:

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

or

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

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

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

将变数传给query_posts

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

例 1

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

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

例 2

在下面的这个例子中,双" 引号 "指示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"); ?>
<!—将你的loop放在这儿 -->

例 3

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

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

参数

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

类别 参数

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

  • cat
  • category_name

根据ID显示一个类别

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

query_posts('cat=4');

根据名称显示一个类别

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

query_posts('category_name=Staff Home');

显示几个类别及ID

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

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

删除某个类别的文章

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

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


标签参数

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

  • tag

为某个标签提取文章

query_posts('tag=cooking');

获得拥有任何这样的标签的文章 query_posts('tag=bread,baking');


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

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

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

作者参数

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

  • author_name=Harriet
  • author=3

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

文章 & 网页参数

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

  • p=1 - 使用文章 ID来显示第一篇文章
  • name=first-post - 使用 post Slug 显示第一篇文章
  • page_id=7
  • pagename=about
  • showposts=1 (你可以使用 showposts=3,或者其它的任何数字显示一定数目的文章

由于 模板层级方面的原因, 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

相关的

模板:标签总标签