几个调用最新文章代码

发布时间:2020-05-05

摘要

This function is used to get a list of all the pages that are defined in the blog. Essentially get_pages gives you an array of the pages, and that array is not tree-like. See the template tag, wp_list_pages(), for the output of page titles in a tree-like fashion.
Note that, although similar to get_posts, several of the parameter names and values differ.

  • Reset after Postlists with offset

目录

说明:

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 $posts_array = get_posts( $args ); ?>   

默认用法:

  1. <?php $args = array(   
  2.     ‘numberposts’     => 5,   
  3.     ‘offset’          => 0,   
  4.     ‘category’        => ,   
  5.     ‘orderby’         => ‘post_date’,   
  6.     ‘order’           => ‘DESC’,   
  7.     ‘include‘         => ,   
  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. 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. ?>  

 

代码源自wordpress官方Codex

大熊wordpress凭借多年的wordpress企业主题制作经验,坚持以“为用户而生的wordpress主题”为宗旨,累计为2000多家客户提供品质wordpress建站服务,得到了客户的一致好评。我们一直用心对待每一个客户,我们坚信:“善待客户,将会成为终身客户”。大熊wordpress能坚持多年,是因为我们一直诚信。我们明码标价(wordpress做网站需要多少钱),从不忽悠任何客户,我们的报价宗旨:“拒绝暴利,只保留合理的利润”。如果您有网站建设、网站改版、网站维护等方面的需求,请立即咨询右侧在线客服或拨打咨询热线:18324743309,我们会详细为你一一解答你心中的疑难。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

相关文章

写给所有做网站的朋友的一封信

写给所有做网站的朋友的一封信

现在就开始执行“1+N”互联网推广和没有开始执行的人,一两天看不出任何区别; 一两个月看来差异也是微乎其微的;但在2-5年的长远时间来看的时候,你的高质量询盘不断增加,你的互联网资产已经建立完成,对手已经很难匹敌,现在你看到这段文字的时候就是最好的开始,现在就是最好的时候,马上开始“1+N”体系的整体互联网推广吧,我们和你一起,开创互联网大未来!

点击查看详情

准备开启WordPress网站建设推广?

我们相信高端漂亮的网站不应该是昂贵的,这就是wordpress对每个人都是免费的原因
wordpress建站免费入门,并提供价格合理的wordpress建站套餐。