WordPress:Template Tags/get posts

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

描述

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

用法

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

例子

拥有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中先前写的五篇文章上,你可以使用这个:

 <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> 


<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>


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,上述的查询应该在拥有几篇文章的类别中使用,否则,就不会有结果。

Access all post data

访问所有的文章数据

Some post-related data is not available to get_posts by default, such as post content through the_content(), or the numeric ID. This is resolved by calling an internal function setup_postdata(), with the $post array as its argument:

默认情况下,得到_文章不能够得到一些与文章相关的数据,例如通过the_内容()的文章内容,或者数字ID。通过调用内部的设置_文章数据()函数,参数是$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; ?>
 

<?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; ?>

To access a post's ID or content without calling setup_postdata(), or in fact any post-specific data (data retained in the posts table), you can use $post->COLUMN, where COLUMN is the table column name for the data. So $post->ID holds the ID, $post->post_content the content, and so on. To display or print this data on your page use the PHP echo command, like so:

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

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

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

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:

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

 <?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; ?>
 


<?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; ?>

Random posts

任意挑选的文章

Display a list of 5 posts selected randomly by using the MySQL RAND() function for the orderby parameter value:

使用MySQL RAND()函数,根据参数值的顺序,显示五篇任意选择的文章列表。

 <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> 


<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>

Show all attachments

显示所有的配置

Do this outside any Loops in your template.

在你的模板中任何的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();
	}
}

?>




<?php

$args = array(
	'post_type' => 'attachment',
	'numberposts' => null,
	'post_status' => null,
	'post_parent' => null, // any parent
	); 
$attachments = 得到_文章($args);
if ($attachments) {
	foreach ($attachments as $post) {
		设置_文章数据($post);
		the_标题();
		the_配置_链接($文章->ID,错误的);
		the_摘录();
	}
}

?>



Show attachments for the current post

显示当期文章的配置

Do this inside WordPress:The_Loop (where $post->ID is available).

The_Loop内执行这个步骤(Loop内拥有$文章->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);
	}
}

?>




<?php

$args = array(
	'post_type' => 'attachment',
	'numberposts' => null,
	'post_status' => null,
	'post_parent' => $post->ID
	); 
$attachments = 得到_文章($args);
if ($attachments) {
	foreach ($attachments as $attachment) {
		echo 应用_过滤器('the_title', $attachment->post_title);
		the_配置_链接($配置->ID, 错误的);
	}
}

?>



Parameters

参数

模板:参数



模板:参数


模板:参数


模板:参数




模板:参数



模板:参数



模板:参数



模板:参数



模板:参数



模板:参数 模板:参数



Related

模板:Tag General Tags

模板:Tag Footer

相关的

模板:标签 总 标签