站长百科 | 数字化技能提升教程 数字化时代生存宝典
首页
数字化百科
电子书
▼
建站程序
开发
服务器
办公软件
开发教程
▼
服务器教程
软件使用教程
运营教程
热门电子书
▼
CSS教程
WordPress教程
导航
程序频道
推广频道
网赚频道
人物频道
网站程序
网页制作
云计算
服务器
CMS
论坛
网店
虚拟主机
cPanel
网址导航
WIKI使用导航
WIKI首页
热点词条
最新资讯
网站程序
站长人物
页面分类
使用帮助
编辑测试
创建条目
网站地图
站长百科导航
站长百科
主机侦探
IDCtalk云说
跨境电商导航
WordPress啦
站长专题
网站推广
网站程序
网站赚钱
虚拟主机
cPanel
网址导航专题
云计算
微博营销
虚拟主机管理系统
开放平台
WIKI程序与应用
美国十大主机
编辑“
WordPress:Template Tags/get posts
”(章节)
人物百科
|
营销百科
|
网赚百科
|
站长工具
|
网站程序
|
域名主机
|
互联网公司
|
分类索引
跳转至:
导航
、
搜索
警告:
您没有登录。如果您做出任意编辑,您的IP地址将会公开可见。如果您
登录
或
创建
一个账户,您的编辑将归属于您的用户名,且将享受其他好处。
反垃圾检查。
不要
加入这个!
== 例子 == ===拥有offset的文章列表=== 如果你设置你的博客,在首页只显示一篇文章,但是同时想要在首页列上链接,连接到类别ID1中先前写的五篇文章上,你可以使用这个: <nowiki> <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> </nowiki> '''注:'''通过使用offset,上述的查询应该在拥有几篇文章的类别中使用,否则,就不会有结果。 ===访问所有的文章数据=== 默认情况下,get_posts不能够得到一些与文章相关的数据,例如通过[[WordPress:Template Tags/the_content|the_content()]]的文章内容,或者数字ID。通过调用内部的setup_postdata()函数,参数是$post array,可以解决这个问题。 <nowiki> <?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; ?> </nowiki> 不使用setup_postdata()访问一篇文章的ID或者内容,或者其它的关于文章的数据([[WordPress:Database_Description|文章表格]]中的数据),你可以使用<tt>$post->''COLUMN''</tt>,<tt>''COLUMN''</tt>是数据的表格栏的名称。因此<tt>$post->ID</tt>持有ID,<tt>$post->post_content</tt>内容,等等。请使用[[WordPress:Glossary#PHP|PHP]] echo 命令行将这个数据输入或者显示在你的网页上,如: <nowiki> <?php echo $post->ID; ?> </nowiki> ===按标题排序的最新的文章=== 以升序,按字母顺序显示最近十篇文章,下面会显示文章日期,标题和摘录: <nowiki> <?php $postslist = get_posts('numberposts=10&order=ASC&orderby=post_title'); foreach ($postslist as $post) : setup_postdata($post); ?> <div> <?php the_date(); ?> <br /> <?php the_title(); ?> <?php the_excerpt(); ?> </div> <?php endforeach; ?> </nowiki> ===任意挑选的文章=== 使用[[WordPress:Glossary#MySQL|MySQL]] RAND()函数,根据参数值的顺序,显示五篇任意选择的文章列表。 <nowiki> <ul><li><h2>任意选择我所写的文章</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> </nowiki> ===显示所有的配置=== 在你的模板中任何的[[WordPress:The_Loop|Loops]]之外,执行这个步骤。 <?php $args = array( 'post_type' => 'attachment', 'numberposts' => null, 'post_status' => null, 'post_parent' => null, // any parent ); $attachments = [[WordPress:Template_Tags/get_posts|get_posts]]($args); if ($attachments) { foreach ($attachments as $post) { [[WordPress:Function_Reference/setup_postdata|setup_postdata]]($post); [[WordPress:Template_Tags/the_title|the_title]](); [[WordPress:Template_Tags/the_attachment_link|the_attachment_link]]($post->ID, false); [[WordPress:Template_Tags/the_excerpt|the_excerpt]](); } } ?> ===显示当期文章的配置=== 在[[WordPress:The_Loop|The_Loop]]内执行这个步骤(Loop内拥有<var>$post->ID</var>)。 <?php $args = array( 'post_type' => 'attachment', 'numberposts' => null, 'post_status' => null, 'post_parent' => $post->ID ); $attachments = [[WordPress:Template_Tags/get_posts|get_posts]]($args); if ($attachments) { foreach ($attachments as $attachment) { echo [[WordPress:Function_Reference/apply_filters|apply_filters]]('the_title', $attachment->post_title); [[WordPress:Template_Tags/the_attachment_link|the_attachment_link]]($attachment->ID, false); } } ?>
摘要:
请注意,您对站长百科的所有贡献都可能被其他贡献者编辑,修改或删除。如果您不希望您的文字被任意修改和再散布,请不要提交。
您同时也要向我们保证您所提交的内容是您自己所作,或得自一个不受版权保护或相似自由的来源(参阅
Wordpress-mediawiki:版权
的细节)。
未经许可,请勿提交受版权保护的作品!
取消
编辑帮助
(在新窗口中打开)