几个调用最新文章代码

说明:

This is a simple function for creating multiple loops. It retrieves a list of latest posts or posts matching criteria.

Note that although the parameters are similar to get_pages, several have different names or take slightly different values (see get_pages).

用法

  1. <?php  = get_posts(  ); ?>   

默认用法:

  1. <?php  = (   
  2.     \’numberposts\’     => 5,   
  3.     \’offset\’          => 0,   
  4.     \’category\’        => ,   
  5.     \’orderby\’         => \’post_date\’,   
  6.     \’order\’           => \’DESC\’,   
  7.     \’\’         => ,   
  8.     \’exclude\’         => ,   
  9.     \’meta_key\’        => ,   
  10.     \’meta_value\’      => ,   
  11.     \’post_type\’       => \’post\’,   
  12.     \’post_mime_type\’  => ,   
  13.     \’post_parent\’     => ,   
  14.     \’post_status\’     => \’publish\’ ); ?>   

参数:

get_posts() makes use of the WP_Query class to fetch posts. See the parameters section of the WP_Query documentation for a list of parameters that this function accepts.

Note: get_posts uses \'suppress_filters\' => true as default, while query_posts() applies filters by default, this can be confusing when using query-modifying plugins, like WPML.

返回值

(array) 
List of post objects. Check get_post() return values.

例子

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.  ;   
  4.  = ( \’numberposts\’ => 5, \’offset\’=> 1, \’category\’ => 1 );   
  5.  = get_posts(  );   
  6.    ) :  setup_postdata(); ?>   
  7.     <li><a href=><?php the_title(); ?></a></li>   
  8. <?php ; ?>   
  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.  ;   
  4.  = ;   
  5.  = ( \’numberposts\’ => 5, \’offset\’=> 1, \’category\’ => 1 );   
  6.  = get_posts(  );   
  7.    ) : setup_postdata(); ?>   
  8.     <li><a href=><?php the_title(); ?></a></li>   
  9. <?php ; ?>   
  10. <?php  = ; ?>   
  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.  = ( \’numberposts\’ => 3 );   
  3.  = get_posts(  );   
  4. (  ) : setup_postdata(); ?>   
  5.     <h2><a href=><?php the_title(); ?></a></h2>   
  6.     <?php the_content(); ?>   
  7. <?php ; ?>  

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  ->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.  = ( \’numberposts\’ => 10, \’order\’=> \’ASC\’, \’orderby\’ => \’title\’ );   
  3.  = get_posts(  );   
  4.  (  ) :  setup_postdata(); ?>    
  5.     <div>   
  6.         <?php the_date(); ?>   
  7.         <br />   
  8.         <?php the_title(); ?>      
  9.         <?php the_excerpt(); ?>   
  10.     </div>   
  11. <?php ; ?>  

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.  = ( \’numberposts\’ => 5, \’orderby\’ => \’rand\’ );   
  4.  = get_posts(  );   
  5.    ) : ?>   
  6.     <li><a href=><?php the_title(); ?></a></li>   
  7. <?php ; ?>   
  8. </ul>  

Show all attachments

Do this outside any Loops in your template.

  1. <?php   
  2.  = ( \’post_type\’ => \’attachment\’, \’numberposts\’ => -1, \’post_status\’ => null, \’post_parent\’ => null );    
  3.  = get_posts(  );   
  4.  () {   
  5.      (    ) {   
  6.         setup_postdata();   
  7.         the_title();   
  8.         the_attachment_link(->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.  = ( \’post_type\’ => \’attachment\’, \’numberposts\’ => -1, \’post_status\’ => null, \’post_parent\’ => ->ID );    
  3.  = get_posts();   
  4.  () {   
  5.      (    ) {   
  6.          apply_filters( \’the_title\’ , ->post_title );   
  7.         the_attachment_link( ->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.  = \’my_slag\’;   
  3. =(   
  4.   \’name\’ => ,   
  5.   \’post_type\’ => \’post\’,   
  6.   \’post_status\’ => \’publish\’,   
  7.   \’showposts\’ => 1,   
  8.   \’caller_get_posts\’=> 1   
  9. );   
  10.  = get_posts();   
  11.  ) {   
  12.  \’ID on the first post found \’.[0]->ID;   
  13. }   
  14. ?>  

代码源自WordPress官方Codex

    © 版权声明
    THE END
    喜欢就支持一下吧
    点赞0 分享
    评论 抢沙发

    请登录后发表评论

      暂无评论内容