不知大家有没碰到这么一个问题,在特定页面,比方首页,比方single页,或者是siderbar.php,想要调用一篇page,或者是一个post。
这个时候该怎么做呢?
原来一直也没找到合适的办法,不过今天发现了这个函数,query posts()。
该函数使用很简单,比如我们要调用id为1的post,就可以这样写:
01.
< ?php
02.
// 返回一篇ID为5的文章
03.
query_posts(
'p=5'
);
04.
//或者你想得到id为1的page
05.
//query_posts('page_id=1'); //只返回page1
06.
//或者这样:
07.
//query_posts('pagename=about'); //只返回title为about的page
08.
//global $more;
09.
//将 $more 设置为0以得到文章的第一部分
10.
//$more = 0;
11.
// the Loop
12.
while
(have_posts()) : the_post();
13.
//文章的内容
14.
the_content(
'Read All»'
);
15.
endwhile
;
16.
?>
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变得更加个性化。
转载请注明:夜阑小雨 » WordPress query posts()函数