WIKI使用導(dǎo)航
站長(zhǎng)百科導(dǎo)航
站長(zhǎng)專題
- 網(wǎng)站推廣
- 網(wǎng)站程序
- 網(wǎng)站賺錢
- 虛擬主機(jī)
- cPanel
- 網(wǎng)址導(dǎo)航專題
- 云計(jì)算
- 微博營(yíng)銷
- 虛擬主機(jī)管理系統(tǒng)
- 開放平臺(tái)
- WIKI程序與應(yīng)用
- 美國(guó)十大主機(jī)
WordPress:Template Tags/get posts
描述[ ]
這是一個(gè)簡(jiǎn)單的標(biāo)簽,是用來創(chuàng)建多個(gè)loops。
用法[ ]
%%% <?php get_posts('arguments'); ?> %%%
例子[ ]
擁有offset的文章列表[ ]
如果你設(shè)置你的博客,在首頁只顯示一篇文章,但是同時(shí)想要在首頁列上鏈接,連接到類別ID1中先前寫的五篇文章上,你可以使用這個(gè):
<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>
注:通過使用offset,上述的查詢應(yīng)該在擁有幾篇文章的類別中使用,否則,就不會(huì)有結(jié)果。
訪問所有的文章數(shù)據(jù)[ ]
默認(rèn)情況下,get_posts不能夠得到一些與文章相關(guān)的數(shù)據(jù),例如通過the_content()的文章內(nèi)容,或者數(shù)字ID。通過調(diào)用內(nèi)部的setup_postdata()函數(shù),參數(shù)是$post array,可以解決這個(gè)問題。
<?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或者內(nèi)容,或者其它的關(guān)于文章的數(shù)據(jù)(文章表格中的數(shù)據(jù)),你可以使用$post->COLUMN,COLUMN是數(shù)據(jù)的表格欄的名稱。因此$post->ID持有ID,$post->post_content內(nèi)容,等等。請(qǐng)使用PHP echo 命令行將這個(gè)數(shù)據(jù)輸入或者顯示在你的網(wǎng)頁上,如:
<?php echo $post->ID; ?>
按標(biāo)題排序的最新的文章[ ]
以升序,按字母順序顯示最近十篇文章,下面會(huì)顯示文章日期,標(biāo)題和摘錄:
<?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; ?>
任意挑選的文章[ ]
使用MySQL RAND()函數(shù),根據(jù)參數(shù)值的順序,顯示五篇任意選擇的文章列表。 <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>
顯示所有的配置[ ]
在你的模板中任何的Loops之外,執(zhí)行這個(gè)步驟。
<?php $args = array( 'post_type' => 'attachment', 'numberposts' => null, 'post_status' => null, 'post_parent' => null, // any parent ); $attachments = get_posts($args); if ($attachments) { foreach ($attachments as $post) { setup_postdata($post); the_title(); the_attachment_link($post->ID, false); the_excerpt(); } } ?>
顯示當(dāng)期文章的配置[ ]
在The_Loop內(nèi)執(zhí)行這個(gè)步驟(Loop內(nèi)擁有$post->ID)。
<?php $args = array( 'post_type' => 'attachment', 'numberposts' => null, '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); } } ?>