WordPress: Template Tags/get posts:修订间差异

来自站长百科
跳转至: 导航、​ 搜索
无编辑摘要
无编辑摘要
 
第10行: 第10行:


===拥有offset的文章列表===
===拥有offset的文章列表===
If you have your blog configured to show just one post on the front page, but also want to list links to the previous five posts in category ID 1, you can use this:


如果你设置你的博客,在首页只显示一篇文章,但是同时想要在首页列上链接,连接到类别ID1中先前写的五篇文章上,你可以使用这个:
如果你设置你的博客,在首页只显示一篇文章,但是同时想要在首页列上链接,连接到类别ID1中先前写的五篇文章上,你可以使用这个:
第24行: 第22行:
  <?php endforeach; ?>
  <?php endforeach; ?>
  </ul> </nowiki>
  </ul> </nowiki>
<nowiki> <ul>
<?php
global $post;
$myposts = 得到_文章('numberposts=5&offset=1&category=1');
foreach($myposts as $post) :
?>
    <li><a href="<?php the_permalink(); ?>"><?php the_标题(); ?></a></li>
<?php endforeach; ?>
</ul> </nowiki>
'''Note:''' With use of the offset, the above query should be used only on a category that has more than one post in it, otherwise there'll be no output.


'''注:'''通过使用offset,上述的查询应该在拥有几篇文章的类别中使用,否则,就不会有结果。
'''注:'''通过使用offset,上述的查询应该在拥有几篇文章的类别中使用,否则,就不会有结果。
===Access all post data===


===访问所有的文章数据===
===访问所有的文章数据===


Some post-related data is not available to get_posts by default, such as post content through [[WordPress:Template Tags/the_content|the_content()]], or the numeric ID. This is resolved by calling an internal function setup_postdata(), with the $post array as its argument:
默认情况下,get_posts不能够得到一些与文章相关的数据,例如通过[[WordPress:Template Tags/the_content|the_content()]]的文章内容,或者数字ID。通过调用内部的setup_postdata()函数,参数是$post array,可以解决这个问题。
 
默认情况下,得到_文章不能够得到一些与文章相关的数据,例如通过[[WordPress:Template Tags/the_content|the_内容()]]的文章内容,或者数字ID。通过调用内部的设置_文章数据()函数,参数是$post array,可以解决这个问题。
 
<nowiki> <?php
$lastposts = get_posts('numberposts=3');
foreach($lastposts as $post) :
    setup_postdata($post);
?>
<h2><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a></h2>
<?php the_content(); ?>
<?php endforeach; ?>
</nowiki>


<nowiki> <?php
<nowiki> <?php
第69行: 第39行:
  </nowiki>
  </nowiki>


To access a post's ID or content without calling setup_postdata(), or in fact any post-specific data (data retained in [[WordPress:Database_Description|the posts table]]), you can use <tt>$post->''COLUMN''</tt>, where <tt>''COLUMN''</tt> is the table column name for the data. So <tt>$post->ID</tt> holds the ID, <tt>$post->post_content</tt> the content, and so on. To display or print this data on your page use the [[WordPress:Glossary#PHP|PHP]] echo command, like so:
不使用setup_postdata()访问一篇文章的ID或者内容,或者其它的关于文章的数据([[WordPress:Database_Description|文章表格]]中的数据),你可以使用<tt>$post->''COLUMN''</tt>,<tt>''COLUMN''</tt>是数据的表格栏的名称。因此<tt>$post->ID</tt>持有ID,<tt>$post->post_content</tt>内容,等等。请使用[[WordPress:Glossary#PHP|PHP]] echo 命令行将这个数据输入或者显示在你的网页上,如:
 
不使用setup_postdata()访问一篇文章的ID或者内容,或者其它的关于文章的数据([[WordPress:Database_Description|文章表格]]中的数据),你可以使用<tt>$post->''COLUMN''</tt>,<tt>''COLUMN''</tt>是数据的表格栏的名称。因此<tt>$文章->ID</tt>持有ID,<tt>$文章->文章_内容</tt>内容,等等。请使用[[WordPress:Glossary#PHP|PHP]] echo 命令行将这个数据输入或者显示在你的网页上,如:
 
<nowiki> <?php echo $post->ID; ?> </nowiki>


<nowiki> <?php echo $post->ID; ?> </nowiki>
<nowiki> <?php echo $post->ID; ?> </nowiki>
===Latest posts ordered by title===


===按标题排序的最新的文章===
===按标题排序的最新的文章===
To show the last ten posts sorted alphabetically in ascending order, the following will display their post date, title and excerpt:


以升序,按字母顺序显示最近十篇文章,下面会显示文章日期,标题和摘录:
以升序,按字母顺序显示最近十篇文章,下面会显示文章日期,标题和摘录:
第100行: 第62行:
  </nowiki>
  </nowiki>


<nowiki>
<?php
$postslist = 得到_posts('numberposts=10&order=ASC&orderby=post_title');
foreach ($postslist as $post) :
    setup_postdata($post);
?>
<div>
<?php the_date(); ?>
<br />
<?php the_title(); ?> 
<?php the_excerpt(); ?>
</div>
<?php endforeach; ?>
</nowiki>
===Random posts===


===任意挑选的文章===
===任意挑选的文章===
Display a list of 5 posts selected randomly by using the [[WordPress:Glossary#MySQL|MySQL]] RAND() function for the orderby parameter value:


使用[[WordPress:Glossary#MySQL|MySQL]] RAND()函数,根据参数值的顺序,显示五篇任意选择的文章列表。
使用[[WordPress:Glossary#MySQL|MySQL]] RAND()函数,根据参数值的顺序,显示五篇任意选择的文章列表。
<nowiki> <ul><li><h2>A random selection of my writing</h2>
    <ul>
<?php
$rand_posts = get_posts('numberposts=5&orderby=RAND()');
foreach( $rand_posts as $post ) :
?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
    </ul>
</li></ul> </nowiki>
<nowiki> <ul><li><h2>任意选择我所写的文章</h2>
<nowiki> <ul><li><h2>任意选择我所写的文章</h2>
     <ul>
     <ul>
第148行: 第76行:
     </ul>
     </ul>
  </li></ul> </nowiki>
  </li></ul> </nowiki>
===Show all attachments===


===显示所有的配置===
===显示所有的配置===
Do this outside any [[WordPress:The_Loop|Loops]] in your template.


在你的模板中任何的[[WordPress:The_Loop|Loops]]之外,执行这个步骤。
在你的模板中任何的[[WordPress:The_Loop|Loops]]之外,执行这个步骤。
第176行: 第100行:
   
   
  ?>
  ?>
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => null,
'post_status' => null,
'post_parent' => null, // any parent
);
$attachments = [[WordPress:Template_Tags/get_posts|得到_文章]]($args);
if ($attachments) {
foreach ($attachments as $post) {
[[WordPress:Function_Reference/setup_postdata|设置_文章数据]]($post);
[[WordPress:Template_Tags/the_title|the_标题]]();
[[WordPress:Template_Tags/the_attachment_link|the_配置_链接]]($文章->ID,错误的);
[[WordPress:Template_Tags/the_excerpt|the_摘录]]();
}
}
?>
===Show attachments for the current post===


===显示当期文章的配置===
===显示当期文章的配置===


Do this inside [[WordPress:The_Loop]] (where <var>$post->ID</var> is available).
在[[WordPress:The_Loop|The_Loop]]内执行这个步骤(Loop内拥有<var>$post->ID</var>)。
 
在[[WordPress:The_Loop|The_Loop]]内执行这个步骤(Loop内拥有<var>$文章->ID</var>)。


  <?php
  <?php
第232行: 第123行:
  ?>
  ?>


<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => null,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = [[WordPress:Template_Tags/get_posts|得到_文章]]($args);
if ($attachments) {
foreach ($attachments as $attachment) {
echo [[WordPress:Function_Reference/apply_filters|应用_过滤器]]('the_title', $attachment->post_title);
[[WordPress:Template_Tags/the_attachment_link|the_配置_链接]]($配置->ID, 错误的);
}
}
?>
== Parameters ==
== 参数 ==
== 参数 ==
{{Parameter|$numberposts|integer|Number of posts to return.|optional|5}}
{{Parameter|$numberposts|integer|Number of posts to return.|optional|5}}
{{参数|$文章数目|整数|返回的文章数目|可选择的|5}}


{{Parameter|$offset|integer|Offset from latest post.|optional|0}}
{{Parameter|$offset|integer|Offset from latest post.|optional|0}}
{{参数|$offset|整数|最新文章的Offset|可选择的|0}}


{{Parameter|$category|integer|Only show posts from this category ID.|optional}}
{{Parameter|$category|integer|Only show posts from this category ID.|optional}}


{{参数|$类别|整数|只显示来自这个类别ID的文章|可选择的}}
{{Parameter|$orderby|string|根据一个参数给文章排序, 参数包括: <ul><li><tt>'post_title'</tt> - 根据网页或者文章标题按照字母顺序给文章分类。</li><li><tt>'post_date'</tt> - 根据文章创建的时间给文章分类。</li><li><tt>'post_modified'</tt> - 根据最后更改的时间给文章分类。</li><li><tt>'ID'</tt> -根据ID,给文章分类。</li><li><tt>'post_author'</tt> - 根据作者的ID,给文章分类。</li><li><tt>'post_name'</tt> -根据文章slug,按字母顺序给文章分类。 </li></ul><br/>'''注''': <tt>$orderby</tt> 参数可以是 [[WordPress:Database Description#Table:_wp_posts|wp_posts table]]任何区的名称。|可选择的|post_title}}
 
{{Parameter|$orderby|string|Sort posts by one of various values, including: <ul><li><tt>'post_title'</tt> - Sort alphabetically by page or post title.</li><li><tt>'post_date'</tt> - Sort by creation time.</li><li><tt>'post_modified'</tt> - Sort by time last modified.</li><li><tt>'ID'</tt> - Sort by numeric post ID.</li><li><tt>'post_author'</tt> - Sort by the numeric IDs of authors.</li><li><tt>'post_name'</tt> - Sort alphabetically by post slug.</li></ul><br/>'''Note''': The <tt>$orderby</tt> value can be the name of any field in the [[WordPress:Database Description#Table:_wp_posts|wp_posts table]].|optional|post_title}}
 
{{参数|$排序|string|根据一个参数给文章排序, 参数包括: <ul><li><tt>'文章_标题'</tt> - 根据网页或者文章标题按照字母顺序给文章分类。</li><li><tt>'文章_日期'</tt> - 根据文章创建的时间给文章分类。</li><li><tt>'文章_更改的'</tt> - 根据最后更改的时间给文章分类。</li><li><tt>'ID'</tt> -根据ID,给文章分类。</li><li><tt>'文章_作者'</tt> - 根据作者的ID,给文章分类。</li><li><tt>'文章_名称'</tt> -根据文章slug,按字母顺序给文章分类。 </li></ul><br/>'''注''': <tt>$顺序</tt> 参数可以是 [[WordPress:Database Description#Table:_wp_posts|wp_文章表格]]任何区的名称。|可选择的|文章_标题}}
 
 
 
 
{{Parameter|$order|string|How to sort <tt>$orderby</tt>. Valid values:<ul><li><tt>'ASC'</tt> - Ascending (lowest to highest).</li><li><tt>'DESC'</tt> - Descending (highest to lowest).</li></ul>|optional|ASC}}
 
 
{{参数|$顺序|字符串|怎样分类 <tt>$分类顺序</tt>。有效的参数值:<ul><li><tt>'ASC'</tt> - 上升的 (从最低到最高)。</li><li><tt>'DESC'</tt> - 下降的 (从最高到最低)。</li></ul>|可选择的|ASC}}
 
 
{{Parameter|$include|string|The IDs of the posts you want to show, separated by commas and/or spaces. The following value would work in showing these six posts:<ul><li>'45,63, 78 94 ,128 , 140'</li></ul>'''Note: '''''Using this parameter will override the numberposts, offset, category, exclude, meta_key, meta_value, and post_parent parameters.''|optional}}
 
{{参数|$包含|字符串|你想要显示的文章的ID,用逗号和/或者空格分开这些ID。下面的参数值能够运行,显示这六篇文章: <ul><li>'45,63, 78 94 ,128 , 140'</li></ul>'''注: '''''使用这个参数,会取消文章数目,offset,类别,exclude, meta_key,meta_value,和post_parent参数。''|可选择的}}
 
 
{{Parameter|$exclude|string|The IDs of any posts you want to exclude, separated by commas and/or spaces (see <tt>$include</tt> parameter).|optional}}
 
{{参数|$排除|字符串|你想要删除的任何一篇文章的ID,这些ID有逗号和/或者空格分开(请看看 <tt>$包含</tt> 参数)。|可选择的}}
 
 
{{Parameter|$meta_key and $meta_value|string|Only show posts that contain a meta (custom) field with this key and value. Both parameters must be defined, or neither will work.|optional}}
 
{{参数|$meta_key and $meta_value|string|只显示包含一个meta(自定义)区拥有这个key和参数值的文章。两个参数都必须定义,而且两个参数都不会运行。|可选择的}}


{{Parameter|$order|字符串|怎样分类 <tt>$分类顺序</tt>。有效的参数值:<ul><li><tt>'ASC'</tt> - 上升的 (从最低到最高)。</li><li><tt>'DESC'</tt> - 下降的 (从最高到最低)。</li></ul>|可选择的|ASC}}


{{Parameter|$post_type|string|The type of post to show. Available options are:<ul><li>post - Default</li><li>page</li><li>attachment</li><li>''(blank)'' - all post types</li></ul>|optional|post}}
{{Parameter|$include|字符串|你想要显示的文章的ID,用逗号和/或者空格分开这些ID。下面的参数值能够运行,显示这六篇文章: <ul><li>'45,63, 78 94 ,128 , 140'</li></ul>'''注: '''''使用这个参数,会取消文章数目,offset,类别,exclude, meta_key,meta_value,和post_parent参数。''|可选择的}}


{{参数|$post_type|string|需要显示的文章的类型。拥有的选项有: <ul><li>文章 – 默认</li><li>网页</li><li>配置</li><li>''(空白的)'' – 所有的文章类型</li></ul>|可供选择的|文章}}
{{Parameter|$exclude|字符串|你想要删除的任何一篇文章的ID,这些ID有逗号和/或者空格分开(请看看 <tt>$include</tt> 参数)|可选择的}}


{{Parameter|$meta_key and $meta_value|string|只显示包含一个meta(自定义)区拥有这个key和参数值的文章。两个参数都必须定义,而且两个参数都不会运行。|可选择的}}


{{Parameter|$post_status|string|Show posts with a particular status. Available options are: <ul><li>publish - Default</li><li>private</li><li>draft</li><li>future</li><li>''(blank)'' - all post types</ul>|optional|publish}}
{{Parameter|$post_type|string|需要显示的文章的类型。拥有的选项有: <ul><li>文章 – 默认</li><li>网页</li><li>配置</li><li>''(空白的)'' – 所有的文章类型</li></ul>|可供选择的|文章}}
{{Parameter|$post_parent|integer|Show only the children of the post with this ID|optional}}


{{参数|$post_status|string|显示特殊级别的文章。拥有的选项有: <ul><li>发表的 – 默认</li><li>,秘密的</li><li>草稿</li><li>将来的</li><li>''(空白的)'' –所有的文章类型</ul>|可选择的|发表}}
{{Parameter|$post_status|string|显示特殊级别的文章。拥有的选项有: <ul><li>发表的 – 默认</li><li>,秘密的</li><li>草稿</li><li>将来的</li><li>''(空白的)'' –所有的文章类型</ul>|可选择的|发表}}
{{参数|$post_parent|整数|只显示这个ID内的文章的子文章。|可选择的}}
{{参数|$post_parent|整数|只显示这个ID内的文章的子文章。|可选择的}}
== Related ==
{{Tag General Tags}}
{{Tag Footer}}


== 相关的 ==
== 相关的 ==

2008年7月13日 (日) 16:51的最新版本

描述[ ]

这是一个简单的标签,是用来创建多个loops。

用法[ ]

%%% <?php get_posts('arguments'); ?> %%%

例子[ ]

拥有offset的文章列表[ ]

如果你设置你的博客,在首页只显示一篇文章,但是同时想要在首页列上链接,连接到类别ID1中先前写的五篇文章上,你可以使用这个:

 <ul>
 <?php
 global $post;
 $myposts = get_posts('numberposts=5&offset=1&category=1');
 foreach($myposts as $post) :
 ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
 <?php endforeach; ?>
 </ul> 

注:通过使用offset,上述的查询应该在拥有几篇文章的类别中使用,否则,就不会有结果。

访问所有的文章数据[ ]

默认情况下,get_posts不能够得到一些与文章相关的数据,例如通过the_content()的文章内容,或者数字ID。通过调用内部的setup_postdata()函数,参数是$post array,可以解决这个问题。

<?php $lastposts = get_posts('numberposts=3'); foreach($lastposts as $post) : setup_postdata($post);  ?> <h2><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a></h2> <?php the_content(); ?> <?php endforeach; ?>

不使用setup_postdata()访问一篇文章的ID或者内容,或者其它的关于文章的数据(文章表格中的数据),你可以使用$post->COLUMNCOLUMN是数据的表格栏的名称。因此$post->ID持有ID,$post->post_content内容,等等。请使用PHP echo 命令行将这个数据输入或者显示在你的网页上,如:

<?php echo $post->ID; ?>

按标题排序的最新的文章[ ]

以升序,按字母顺序显示最近十篇文章,下面会显示文章日期,标题和摘录:

 <?php
 $postslist = get_posts('numberposts=10&order=ASC&orderby=post_title');
 foreach ($postslist as $post) : 
    setup_postdata($post);
 ?> 
 <div>
 <?php the_date(); ?>
 <br />
 <?php the_title(); ?>   
 <?php the_excerpt(); ?>
 </div>
 <?php endforeach; ?>
 


任意挑选的文章[ ]

使用MySQL RAND()函数,根据参数值的顺序,显示五篇任意选择的文章列表。 <ul><li><h2>任意选择我所写的文章</h2> <ul> <?php $rand_posts = get_posts('numberposts=5&orderby=RAND()'); foreach( $rand_posts as $post ) :  ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> </li></ul>

显示所有的配置[ ]

在你的模板中任何的Loops之外,执行这个步骤。

<?php

$args = array(
	'post_type' => 'attachment',
	'numberposts' => null,
	'post_status' => null,
	'post_parent' => null, // any parent
	); 
$attachments = get_posts($args);
if ($attachments) {
	foreach ($attachments as $post) {
		setup_postdata($post);
		the_title();
		the_attachment_link($post->ID, false);
		the_excerpt();
	}
}

?>

显示当期文章的配置[ ]

The_Loop内执行这个步骤(Loop内拥有$post->ID)。

<?php

$args = array(
	'post_type' => 'attachment',
	'numberposts' => null,
	'post_status' => null,
	'post_parent' => $post->ID
	); 
$attachments = get_posts($args);
if ($attachments) {
	foreach ($attachments as $attachment) {
		echo apply_filters('the_title', $attachment->post_title);
		the_attachment_link($attachment->ID, false);
	}
}

?>

参数[ ]

模板:参数

相关的[ ]

模板:标签 总 标签