站长百科 | 数字化技能提升教程 数字化时代生存宝典
首页
数字化百科
电子书
▼
建站程序
开发
服务器
办公软件
开发教程
▼
服务器教程
软件使用教程
运营教程
热门电子书
▼
CSS教程
WordPress教程
导航
程序频道
推广频道
网赚频道
人物频道
网站程序
网页制作
云计算
服务器
CMS
论坛
网店
虚拟主机
cPanel
网址导航
WIKI使用导航
WIKI首页
热点词条
最新资讯
网站程序
站长人物
页面分类
使用帮助
编辑测试
创建条目
网站地图
站长百科导航
站长百科
主机侦探
IDCtalk云说
跨境电商导航
WordPress啦
站长专题
网站推广
网站程序
网站赚钱
虚拟主机
cPanel
网址导航专题
云计算
微博营销
虚拟主机管理系统
开放平台
WIKI程序与应用
美国十大主机
编辑“
WordPress常用函数get posts
”
人物百科
|
营销百科
|
网赚百科
|
站长工具
|
网站程序
|
域名主机
|
互联网公司
|
分类索引
跳转至:
导航
、
搜索
警告:
您没有登录。如果您做出任意编辑,您的IP地址将会公开可见。如果您
登录
或
创建
一个账户,您的编辑将归属于您的用户名,且将享受其他好处。
反垃圾检查。
不要
加入这个!
<span style="border:1px solid #000; text-align:center; float:right; padding:6px;"><strong>导航:</strong> [[WordPress常用函数(上)| 上一页]] | {{Template:WordPress导航}}</span> <div style="clear:both;"></div> ==说明== 这是一个用于创建多环路的简单标签。 ==用法== <?php get_posts('arguments'); ?> ==示例== ===最初到现在的文章列表=== 如果在博客首页上只设置显示一篇文章,但同时希望在分类ID 1中显示最近五篇文章的链接,可使用如下代码: <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> <STRONG>注意</STRONG>:<EM>使用offset时,以上查询仅适用于含有一篇以上文章的分类,否则无法输出。</EM> ===获取所有文章资料=== 默认情况下get_posts无法获取一些文章相关数据,如通过 [http://codex.wordpress.org/Template_Tags/the_content the_content()]获取文章内容或序列ID。调用内部函数setup_postdata(),以$post 数组为其自变量,可以解决这一问题: <?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; ?> 不希望通过调用setup_postdata()来获取文章的ID或内容,或者获取文章的任何相关数据时(数据存留在[http://codex.wordpress.org/Database_Description 文章列表]中),可以使用$post->COLUMN,COLUMN是文章数据表格的纵列名称。因此$post->ID指明文章ID,$post->post_content指明文章内容,以此类推。如要在页面上显示这些数据,请使用[http://codex.wordpress.org/Glossary#PHP PHP] echo命令,如下所示: <PRE><?php echo $post->ID; ?> </PRE> ===按标题为最新发表文章排序=== 以下代码可按字母升序显示最近发表的十篇文章的发布日期、标题和摘要: <?php $postslist = get_posts('numberposts=10&order=ASC&orderby=title'); foreach ($postslist as $post) : setup_postdata($post); ?> <div> <?php the_date(); ?> <br /> <?php the_title(); ?> <?php the_excerpt(); ?> </div> <?php endforeach; ?> <STRONG>注意</STRONG>:<EM>排序参数在[http://codex.wordpress.org/Version_2.6 2.6版本]中有所修改。此代码适用于新排序格式。详细内容参见[http://codex.wordpress.org/Template_Tags/get_posts#Parameters 参数]。</EM> ===任意文章=== 用[http://codex.wordpress.org/Glossary#MySQL MySQL] RAND()函数指定排序参数的值,可以显示出随意选择的五篇文章: <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> ===显示所有附件=== 不用模板中任何循环进行本项操作。 (使用2.5版本后的get_children()函数相对方便。) <?php $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => null, // any parent); $attachments = <STRONG>get_posts</STRONG>($args); if ($attachments) { foreach ($attachments as $post) { [http://codex.wordpress.org/index.php?title=Function_Reference/setup_postdata&action=edit&redlink=1 setup_postdata]$post); [http://codex.wordpress.org/Template_Tags/the_title the_title](); [http://codex.wordpress.org/Template_Tags/the_attachment_link the_attachment_link]($post->ID, false); [http://codex.wordpress.org/Template_Tags/the_excerpt the_excerpt](); } } ?> ===显示最新文章的附件=== 在[http://codex.wordpress.org/The_Loop The_Loop] ($post->ID可用)中进行本类操作。 <?php $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID ); $attachments = <STRONG>get_posts</STRONG>($args); if ($attachments) { foreach ($attachments as $attachment) { echo [http://codex.wordpress.org/Function_Reference/apply_filters apply_filters]('the_title', $attachment->post_title); [http://codex.wordpress.org/Template_Tags/the_attachment_link the_attachment_link]($attachment->ID, false); } } ?> ==参数:WordPress 2.6+== 除“[http://codex.wordpress.org/Template_Tags/get_posts#Parameters:_WordPress_2.5_And_Older WordPress 2.5及更早版本”]中列出的参数外,get_posts( )也能运行query_posts( )所操作的参数,目前这两个函数在内部使用相同的数据库查询代码。 <STRONG>注意</STRONG>:2.6版本对一些排序选项做了更改。表格字段前不再含有post_字样。如post_title已被改为title,post_data改为data。 ==参数:WordPress 2.5及更早版本== <STRONG>$numberposts</STRONG> (整数)(可选)将要返回的文章数量。将其设为0可在每页上显示最大数量文章数,设为-1可消除限制。 默认值:5 <STRONG>$offset</STRONG> (整数)(可选)以最新文章为起始位 默认值:0 <STRONG>$category</STRONG> (整数)(可选)仅显示本分类编号下的文章。将分类编号设为负数(如果是3,设为-3),显示结果不匹配。用逗号将分类编号隔开,或传递编号数组,可指定多个分类编号。 默认值:None <STRONG>$category_name</STRONG> (字符)(可选)仅显示本分类名称或分类缩略名下的文章。 默认值:None <STRONG>$tag</STRONG> (字符)(可选)仅显示本标签缩略名下的文章。若指定多个用逗号隔开的标签缩略名,则返回结果是:所有文章都与某个标签匹配。若指定多个用空格隔开的标签缩略名,返回结果是:所有文章都与指定标签缩略名匹配。 默认值:None <STRONG>$orderby</STRONG> (字符)(可选)按不同值(用空格隔开)为文章排序,包括: *'author' —— 按作者数值编号排序 *'category' —— 按类别数值编号排序 *'content' —— 按内容排序 *'date' —— 按创建日期排序 *'ID' —— 按文章编号排序 *'menu_order' —— 按菜单顺序排序。仅页面可用。 *'mime_type' —— 按MIME类型排序。仅附件可用。 *'modified' —— 按最后修改时间排序。 *'name' —— 按存根排序。 *'parent' —— 按父级ID排序 *'password' —— 按密码排序 *'rand' —— 任意排序结果 *'status' —— 按状态排序 *'title' —— 按标题排序 *'type' —— 按类型排序 <STRONG>注意</STRONG>: *按编号排序和任意排序自[http://codex.wordpress.org/Version_2.5 2.5版本]起启用。 默认值: post_date <STRONG>$order</STRONG> (字符)(可选)如何对$order排序。可能的值为: *'ASC' —— 升序 (低到高) *'DESC' —— 降序 (高到底) 默认值:DESC <STRONG>$include</STRONG> (字符)(可选)希望显示的文章编号,用逗号和/或空格隔开。显示六篇文章时,下列值可能生效: *'45,63, 78 94,128, 140' <STRONG>注意</STRONG>:该参数将改写numberposts,offset,category,exclude,meta_key,meta_value,及post_parent参数。 默认值:None <STRONG>$exclude</STRONG> (字符)(可选)不希望显示的文章编号,用逗号和/或空格隔开(参见$include参数)。 默认值:None <STRONG>$meta_key 和 $meta_value</STRONG> (字符)(可选)仅显示含有该关键词和值的元(自定义)字段的文章。两项参数都应定义,否则无法运行。 默认值:None <STRONG>$post_type</STRONG> (字符)(可选)希望显示的文章类型。可选项有: *post —— 默认 *page *attachment *any —— 任意文章类型 默认值:post <STRONG>$post-status</STRONG> (字符)(可选)显示特定状态的文章。可选项有: *publish *private *draft *future *inherit —— 若$post_type设为附件,则此项为默认选项 *(blank) —— 所有状态 默认值:publish <STRONG>$post_parent</STRONG> (整数)(可选)显示此文章编号下的子文章 默认值:None <STRONG>$nopaging</STRONG> (布尔型)(可选)激活或禁用分页功能。如果禁用,$numberposts选项被略过。 默认值:None ==相关条目== *[[Drupal]] *[[OBLOG]] *[[X-Space]] *[[SaBlog-X]] *[[Bo-Blog]] [[category:WordPress中文文档|G]] [[category:WordPress网站开发|G]] [[category:WordPress开发文档|G]]
摘要:
请注意,您对站长百科的所有贡献都可能被其他贡献者编辑,修改或删除。如果您不希望您的文字被任意修改和再散布,请不要提交。
您同时也要向我们保证您所提交的内容是您自己所作,或得自一个不受版权保护或相似自由的来源(参阅
Wordpress-mediawiki:版权
的细节)。
未经许可,请勿提交受版权保护的作品!
取消
编辑帮助
(在新窗口中打开)
本页使用的模板:
模板:WordPress导航
(
查看源代码
)(受保护)