WordPress企业主题定制/开发/优化

wordpress之query_posts()函数

首页 » WORDPRESS主题技术 » wordpress之query_posts()函数

query_posts()函数

query_posts()函数结合适当的参数用来控制哪些文章会在页面上显示。

形如query_posts(”cat=3,6&cat=-5,-10″)表示取分类ID为3和6的文章显示,不取分类ID为5和10的文章显示;

形如query_posts(”order=ASC&showposts=10&offset=1&orderby=date&posts_per_page=5″)意义如下:
order=ASC表示按照升序排列,取为DESC则表示按降序;
showposts=10则表示获取10篇文章;
offset=1表示取最新的文章;
orderby=date表示将文章按照日期排序;
posts_per_page=5表示每页显示5篇文章。

< ?php
// 返回一篇ID为5的文章
query_posts('p=5');
//或者你想得到id为1的page
//query_posts('page_id=1');      //只返回page1
//或者这样:
//query_posts('pagename=about');      //只返回title为about的page
//global $more;
//将 $more 设置为0以得到文章的第一部分
//$more = 0;
// the Loop
while (have_posts()) : the_post();
//文章的内容
the_content('Read All»');
endwhile;
?>

query posts()更高阶的用法就是传参了,可以写一个$sql当做它的参数,如:
1.< ?php query_posts("cat=22&year=$current_year&monthnum=$current_month&order=ASC"); ?>

还有tag的
1.< ?php query_posts('tag=bread+baking+recipe'); ?>

等等。
使用该函数可以让theme变得更加个性化。
query_posts()函数后面可以跟众多种类的参数,功能十分强大,在此,我们不进行过多介绍。

相关项目

  • WordPress外贸企业主题

  • 最近更新

  • 热门标签