wordpress二次开发:神奇函数setup_postdata()

2015-01-17 23:03 阅读 1,251 次 评论关闭

setup_postdata() 函数WordPress 中一个比较神奇的函数,能将对象变量声明到全局的$post变量中,这样我们就可以像在主循环中为所欲为的使用模板标签了。

setup_postdata()函数描述

将我们自定义查询的文章对象声明到全局$post变量中,以便我们使用模板标签。使用方法:

setup_postdata( $post );

这里的查询变量我们需要从get_post()或是get_posts查询获得,当然只要是标准的文章对象,你都可以用setup_postdata来声明到全局去,不仅限于这两个函数。

使用方法详解:

<?php 
//获取全局变量,
global $post;
setup_postdata( $post ); 
?>

官方的例子就是这么坑爹。来一个复杂点的:

<ul>
<?php
 
//定义查询条件
$args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );
 
//获得查询文章(多篇)
$myposts = get_posts( $args );
 
//遍历文章数组
foreach( $myposts as $post ) :	setup_postdata($post); ?>
	setup_postdata( $post ); 
	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
 
<?php 
endforeach;
 
//重置 算是setup_postdata( )的反函数
wp_reset_postdata(); 
 
?>
</ul>

这样是不是就清晰多了?

再来一个例子吧,有关主题的边栏的随机/最新 文章显示模块:

function dw_side_post($i=3){
	global $post;
	$mylist = ''; 
	$mytitle = is_single() ? '最新文章':'随机推荐';
	$posts = is_single() ? get_posts("numberposts={$i}&orderby=post_date"):get_posts("numberposts={$i}&orderby=rand");
	foreach($posts as $post) {
		setup_postdata($post);
		$mylist .=  '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
	}
	 $output = '<div class="widget"><div class="title">'.$mytitle.'</div><ul>'.$mylist.'</ul></div>';
echo $output;
}

中间栏的随机文章和标题评论控制是用这些函数:

  • $args = array( ‘numberposts’ => 5, ‘orderby’ => ‘rand’, ‘category’ => 7 );  
  • $rand_posts = get_posts( $args );  
  • foreach$rand_posts as $post ) : setup_postdata($post); ?>  

来写英文版的例子,以下几个代码是调用最新文章的:

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:

  1. <ul>   
  2. <?php   
  3. global $post;   
  4. $args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );   
  5. $myposts = get_posts( $args );   
  6. foreach$myposts as $post ) :  setup_postdata($post); ?>   
  7.     <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>   
  8. <?php endforeach; ?>   
  9. </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.

Reset after Postlists with offset

If you need after the loop, the post you had before joining the foreach, you can use this:

  1. <ul>   
  2. <?php   
  3. global $post;   
  4. $tmp_post = $post;   
  5. $args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );   
  6. $myposts = get_posts( $args );   
  7. foreach$myposts as $post ) : setup_postdata($post); ?>   
  8.     <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>   
  9. <?php endforeach; ?>   
  10. <?php $post = $tmp_post; ?>   
  11. </ul>  

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:

  1. <?php   
  2. $args = array( 'numberposts' => 3 );   
  3. $lastposts = get_posts( $args );   
  4. foreach($lastposts as $post) : setup_postdata($post); ?>   
  5.     <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>   
  6.     <?php the_content(); ?>   
  7. <?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:

  1. <?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

  1. <?php   
  2. $args = array( 'numberposts' => 10, 'order'=> 'ASC', 'orderby' => 'title' );   
  3. $postslist = get_posts( $args );   
  4. foreach ($postslist as $post) :  setup_postdata($post); ?>    
  5.     <div>   
  6.         <?php the_date(); ?>   
  7.         <br />   
  8.         <?php the_title(); ?>      
  9.         <?php the_excerpt(); ?>   
  10.     </div>   
  11. <?php endforeach; ?>  

Random posts

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

  1. <ul>   
  2. <?php   
  3. $args = array( 'numberposts' => 5, 'orderby' => 'rand' );   
  4. $rand_posts = get_posts( $args );   
  5. foreach$rand_posts as $post ) : ?>   
  6.     <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>   
  7. <?php endforeach; ?>   
  8. </ul>  

Show all attachments

Do this outside any Loops in your template.

  1. <?php   
  2. $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => null );    
  3. $attachments = get_posts( $args );   
  4. if ($attachments) {   
  5.     foreach ( $attachments as $post ) {   
  6.         setup_postdata($post);   
  7.         the_title();   
  8.         the_attachment_link($post->ID, false);   
  9.         the_excerpt();   
  10.     }   
  11. }   
  12. ?> 

Show attachments for the current post

Do this inside The Loop (where $post->ID is available).

  1. <?php   
  2. $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID );    
  3. $attachments = get_posts($args);   
  4. if ($attachments) {   
  5.     foreach ( $attachments as $attachment ) {   
  6.         echo apply_filters( 'the_title' , $attachment->post_title );   
  7.         the_attachment_link( $attachment->ID , false );   
  8.     }   
  9. }   
  10. ?>  

Get a post by its slug

Allows you to get a post ID by post slug. The caller_get_posts argument excludes sticky posts from this custom query.

  1. <?php   
  2. $the_slug = 'my_slag';   
  3. $args=array(   
  4.   'name' => $the_slug,   
  5.   'post_type' => 'post',   
  6.   'post_status' => 'publish',   
  7.   'showposts' => 1,   
  8.   'caller_get_posts'=> 1   
  9. );   
  10. $my_posts = get_posts($args);   
  11. if$my_posts ) {   
  12. echo 'ID on the first post found '.$my_posts[0]->ID;   
  13. }   
  14. ?>  

setup_postdata()函数使用注意事项

使用本函数的时候,请注意不要在主循环中使用,不然会达不到你预期的效果,怎样退出循环,这个你可以在调用该函数不成功的时候去搜搜。

版权声明:本文著作权归原作者所有,欢迎分享本文,谢谢支持!
转载请注明:wordpress二次开发:神奇函数setup_postdata() | 猎微网

评论已关闭!