WordPress:Template Tags/get posts

来自站长百科
Xxf3325讨论 | 贡献2008年6月25日 (三) 11:57的版本 (新页面: == Description == This is a simple tag for creating multiple loops. == Usage == %%% <?php get_posts('arguments'); ?> %%% == Examples == ===Posts list with offset=== If you have your...)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航、​ 搜索

Description

This is a simple tag for creating multiple loops.

Usage

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

Examples

Posts list with 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:

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

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.

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:

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

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

Random posts

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

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

Show all attachments

Do this outside any Loops in your template.

<?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();
	}
}

?>

Show attachments for the current post

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

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

?>

Parameters

Related

模板:Tag General Tags

模板:Tag Footer