站长百科 | 数字化技能提升教程 数字化时代生存宝典
首页
数字化百科
电子书
▼
建站程序
开发
服务器
办公软件
开发教程
▼
服务器教程
软件使用教程
运营教程
热门电子书
▼
CSS教程
WordPress教程
导航
程序频道
推广频道
网赚频道
人物频道
网站程序
网页制作
云计算
服务器
CMS
论坛
网店
虚拟主机
cPanel
网址导航
WIKI使用导航
WIKI首页
热点词条
最新资讯
网站程序
站长人物
页面分类
使用帮助
编辑测试
创建条目
网站地图
站长百科导航
站长百科
主机侦探
IDCtalk云说
跨境电商导航
WordPress啦
站长专题
网站推广
网站程序
网站赚钱
虚拟主机
cPanel
网址导航专题
云计算
微博营销
虚拟主机管理系统
开放平台
WIKI程序与应用
美国十大主机
编辑“
WordPress常用函数get posts
”(章节)
人物百科
|
营销百科
|
网赚百科
|
站长工具
|
网站程序
|
域名主机
|
互联网公司
|
分类索引
跳转至:
导航
、
搜索
警告:
您没有登录。如果您做出任意编辑,您的IP地址将会公开可见。如果您
登录
或
创建
一个账户,您的编辑将归属于您的用户名,且将享受其他好处。
反垃圾检查。
不要
加入这个!
==示例== ===最初到现在的文章列表=== 如果在博客首页上只设置显示一篇文章,但同时希望在分类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-mediawiki:版权
的细节)。
未经许可,请勿提交受版权保护的作品!
取消
编辑帮助
(在新窗口中打开)