说明:
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).
用法
- <?php $posts_array = get_posts( $args ); ?>
默认用法:
- <?php $args = array(
- 'numberposts' => 5,
- 'offset' => 0,
- 'category' => ,
- 'orderby' => 'post_date',
- 'order' => 'DESC',
- 'include' => ,
- 'exclude' => ,
- 'meta_key' => ,
- 'meta_value' => ,
- 'post_type' => 'post',
- 'post_mime_type' => ,
- 'post_parent' => ,
- '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:
- <ul>
- <?php
- global $post;
- $args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );
- $myposts = get_posts( $args );
- foreach( $myposts as $post ) : setup_postdata($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.
Reset after Postlists with offset
If you need after the loop, the post you had before joining the foreach, you can use this:
- <ul>
- <?php
- global $post;
- $tmp_post = $post;
- $args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );
- $myposts = get_posts( $args );
- foreach( $myposts as $post ) : setup_postdata($post); ?>
- <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
- <?php endforeach; ?>
- <?php $post = $tmp_post; ?>
- </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:
- <?php
- $args = array( 'numberposts' => 3 );
- $lastposts = get_posts( $args );
- foreach($lastposts as $post) : setup_postdata($post); ?>
- <h2><a href="<?php the_permalink(); ?>"><?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
- $args = array( 'numberposts' => 10, 'order'=> 'ASC', 'orderby' => 'title' );
- $postslist = get_posts( $args );
- 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>
- <?php
- $args = array( 'numberposts' => 5, 'orderby' => 'rand' );
- $rand_posts = get_posts( $args );
- foreach( $rand_posts as $post ) : ?>
- <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
- <?php endforeach; ?>
- </ul>
Show all attachments
Do this outside any Loops in your template.
- <?php
- $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => null );
- $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 The Loop (where $post->ID is available).
- <?php
- $args = array( 'post_type' => 'attachment', 'numberposts' => -1, '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 );
- }
- }
- ?>
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.
- <?php
- $the_slug = 'my_slag';
- $args=array(
- 'name' => $the_slug,
- 'post_type' => 'post',
- 'post_status' => 'publish',
- 'showposts' => 1,
- 'caller_get_posts'=> 1
- );
- $my_posts = get_posts($args);
- if( $my_posts ) {
- echo 'ID on the first post found '.$my_posts[0]->ID;
- }
- ?>
代码源自WordPress官方Codex
本站文章大部分为原创,用于个人学习记录,可能对您有所帮助,仅供参考!
广东省茂名市电白县 1F
我是沙发呀,最新文章代码网上一大推。
浙江省温州市平阳县 2F
夜深来看看,有意外收获,先学习学习!!
河南省信阳市固始县 3F
欧耶,鸟哥写新文章了!!
陕西省西安市 4F
我看不懂代码啊
江苏省南京市 5F
很实用
美国 6F
这是copy paste吗?
浙江省杭州市 7F
能比较好的 ,做到优化的效果
云南省大理州 8F
知更鸟,请教一个问题:
如何获取当前文章的分类?
尝试过the_category(); get_category_link();wp_list_categories();等等
效果都很不如意
只需要输出锚链接,不要文字部分
我想在文章页获取当前文章的分类链接,用下面的形式:
[a href=当前文章所属分类的链接] {img src=一张“阅读更多”的图片} [/a]
会不会是自己已经思维局限了呢?希望知更鸟指点一下:)
中国 B1
@ 陨落de芳菲 实现方法很简单给默认分类函数加参数
<?php the_category(‘ ’, ‘ ‘, ‘ ‘) ?>
并给这句函数加上标签并定义属性
可以参考我主题的文章编辑按钮
云南省大理州 B2
@ 知更鸟 请问能不能给出一个现成的例子呢?
我昨晚尝试了一下你说的思路,完全不会 – – ||
最后我使用foreach,将get_the_category()设成get_category_link()参数实现
放于文章循环内,只是这代码看起来实在是太那啥了 = v =
压缩包在这里,贴贴贴贴贴:
www.libertystudio.net/catlink.zip
北京市海淀区 9F
我想问下 你这里的代码 dp-highlighter 里面的 ul的样式 是后台添加文章加的样式 还是编辑器改变的呀?麻烦告诉下 谢谢了
四川省成都市 10F
鸟哥、第一次使用HotNews、非常喜欢,也有很多不清楚不懂得问题、请问tab的菜单如何才能把本月排行换成最新文章、把年度排行换成本月排行呢??
中国 B1
@ 米米鸡 打开sidebar.php
大概第5行的
tab_h.php
改为:
tab.php
四川省成都市 B2
@ 知更鸟 谢谢、我看到了一篇帖子、可以在logo旁边加微博图标、可否把公告栏右边的腾讯微博和新浪微博删掉呢?在哪删呢?
重庆市 11F
鸟哥,您好,我一直在用您的Ality主题,可是我一直不懂代码,我一直想让首页最新文章(24小时)标题或者日期显示红色,不知道鸟哥能不能发个这个代码,谢谢了!
中国 B1
@ 自赚网 不懂代码?但你会删除我的主题版权
方法给你写了
https://zmingcx.com/article-style-for-a-certain-period-of-time.html