- 浏览: 13763676 次
- 性别:
- 来自: 洛杉矶
-
文章分类
- 全部博客 (1994)
- Php / Pear / Mysql / Node.js (378)
- Javascript /Jquery / Bootstrap / Web (435)
- Phone / IOS / Objective-C / Swift (137)
- Ubuntu / Mac / Github / Aptana / Nginx / Shell / Linux (335)
- Perl / Koha / Ruby / Markdown (8)
- Java / Jsp (12)
- Python 2 / Wxpython (25)
- Codeigniter / CakePHP (32)
- Div / Css / XML / HTML5 (179)
- WP / Joomla! / Magento / Shopify / Drupal / Moodle / Zimbra (275)
- Apache / VPN / Software (31)
- AS3.0/2.0 / Flex / Flash (45)
- Smarty (6)
- SEO (24)
- Google / Facebook / Pinterest / SNS (80)
- Tools (22)
最新评论
-
1455975567:
xuezhongyu01 写道wocan23 写道我想问下那个 ...
Mysql: LBS实现查找附近的人 (两经纬度之间的距离) -
xuezhongyu01:
wocan23 写道我想问下那个111.1是怎么得来的我也看不 ...
Mysql: LBS实现查找附近的人 (两经纬度之间的距离) -
18335864773:
试试 pageoffice 在线打开 PDF 文件吧. pag ...
jquery在线预览PDF文件,打开PDF文件 -
青春依旧:
opacity: 0.5; 个人喜欢这种方式!关于其他css特 ...
css透明度的设置 (兼容所有浏览器) -
July01:
推荐用StratoIO打印控件,浏览器和系统的兼容性都很好,而 ...
搞定网页打印自动分页问题
今天说说WordPress 的主查询函数 -query_posts(),因为我正在制作的主题里面多次用到了这个函数 。
query_posts()查询函数决定了哪些文章出现在WordPress 主 循环(loop)中,正因为如此,query_posts函数仅用于修改主页循环(Loop),而不是在页面上生成次级循环。如果你希望在主循环外另外生 成循环,应该新建独立的WP_Query对象,用这些对象生成循环。在主循环外的循环上使用query_posts会导致主循环运行偏差,并可能在页面上 显示出你不希望看到的内容。
query_posts()查询函数函数接收大量参数,格式与URL中的参数格式相同(如p=4表示ID为4的文章)。下面就举例说说query_posts函数的一些常用的语法格式。
1.从博客主页上排除某些分类目录
将以下代码添加到index.php文件中,使主页显示的文章可以来自除分类3以外的任何分类。
<?php if (is_home()) { query_posts("cat=-3"); } ?>
你也可以多排除几个分类。
<?php if (is_home()) { query_posts("cat=-1,-2,-3"); } ?>
2.查询指定文章
用以下语句检索某篇指定文章:
<?php //获取ID值为5的文章 query_posts('p=5'); ?>
如果你希望在查询语句中使用Read More功能,请将全局变量$more设为0。
<?php //获取ID值为5的页面 query_posts('p=5'); global $more; //初始化$more $more = 0; //循环查询到的结果 while (have_posts()) : the_post(); the_content('Read the full post ?'); endwhile; ?>
3.检索指定页面
用以下语句检索某篇指定页面:
<?php query_posts('page_id=7'); //获取页面ID为7的页面 ?>
或者
<?php query_posts('pagename=about'); ?>
检索子页面时,需要提供子页面及其父页面的别名,用斜线隔开两者。例如:
<?php query_posts('pagename=parent/child'); ?>
上面都是采取 query_posts($query_string) 的形式来调用该函数,下面介绍另一种方法,用数组传递参数变量。
query_posts(array( 'cat' => 22, 'year' => $current_year, 'monthnum' => $current_month, 'order' => 'ASC', ));
相比字符串方式,数组形式更加形象直观,不容易出错。
下面整理一些经常要用到的参数,有些是我用过的,有些则没有,算作归纳吧。
分类参数
只显示特定分类下的文章。
- cat —— 必须使用分类ID
- category_name
- category_and —— 必须使用分类ID
- category_in —— 必须使用分类ID
- category_not_in —— 必须使用分类ID
根据ID显示单个分类
只显示来自某一个分类目录ID(以及该分类目录下的子分类目录)的文章:
query_posts('cat=4');
根据分类名称显示单个分类
只显示来自某一个分类名称下的文章:
query_posts('category_name=Staff Home');
根据ID显示多个分类
显示来自若干指定分类目录ID下的文章:
query_posts('cat=2,6,17,38');
排除某一分类中的文章
显示除某一分类文章外的所有文章,被排除的分类ID以减号(’-')作为前缀。
query_posts('cat=-3');
以上代码删除ID为3的分类中的文章。
处理多个分类
显示隶属于多个分类的文章。下面的代码可展示同时属于分类2和分类6的文章:
query_posts(array('category__and' => array(2,6)));
如果希望显示分类2或分类6中的文章,可以使用上面介绍的cat,也可以使用category_in函数 (注意这里不会显示分类下子分类中的文章) :
query_posts(array('category__in' => array(2,6)));
可以用下面这种方式排除多个分类中的文章:
query_posts(array('category__not_in' => array(2,6)));
标签参数
显示特定标签下的文章。
- tag —— 必须使用标签ID
- tag_id —— 必须使用标签ID
- tag_and —— 必须使用标签ID
- tag_in —— 必须使用标签ID
- tag_not_in —— 必须使用标签ID
- tag_slug_and ——必须使用标签ID
- tag_slug_in ——必须使用标签ID
获取某一标签中的文章
query_posts('tag=cooking');
获取若干标签中任一标签中的文章
query_posts('tag=bread+baking+recipe');
多个标签
显示同时属于ID为37和47的标签下的文章:
query_posts(array('tag__and' => array(37,47));
若要显示ID为为37或47的标签下的文章,可以使用tag参数,也可以用tag_in:
query_posts(array('tag__in' => array(37,47));
显示的文章既不属于标签37,也不属于标签47:
query_posts(array('tag__not_in' => array(37,47));
tag_slug_in与tag_slug_and工作方式几乎一致,不同之处在于相匹配的别名不同。
作者参数
你也可以根据作者来选择文章。
- author=3
- author=-3 ——排除ID为3的作者所发表的文章
- author_name=Harriet
注意:author_name运行在user_nicename字段上,同时author运行在author id字段上。
显示ID为1的作者所发表的所有页面,以标题顺序排列页面,页面列表上方无置顶文章:
query_posts('caller_get_posts=1&author=1&post_type=page&post_status=publish&orderby=title&order=ASC');
文章&页面参数
检索单篇文章或页面。
- ‘p’ => 27 —— 通过文章ID显示该文章
- ‘name’ => ‘about-my-life’ —— 对某篇文章的查询,查询中含有文章别名
- ‘page_id’ => 7 —— 对ID为7的页面的查询
- ‘pagename’ => ‘about’ —— 注意,这不是页面标题,而是页面路径
- 用’posts_per_page’ => 1 – use ‘posts_per_page’ => 3 展示3篇文章。用’posts_per_page’ => -1展示所有文章
- ‘showposts’ => 1 – use ‘showposts’ => 3 展示3篇文章。用’showposts’ => -1展示所有文章。已弃用。
- ‘post__in’ => array(5,12,2,14,7) —— 指定希望检索的文章ID
- ‘post__not_in’ => array(6,2,8) ——排除不希望检索的文章ID
- ‘post_type’ => ‘page’ ——返回页面;默认值为post;可用值包括any, attachment, page, post或revision。any可检索到除修订版外的所有页面类型。
- ‘post_status’ => ‘publish’ —— 返回已发布页面。可用值还包括pending, draft, future, private, trash。关于inherit请见get_children。trash状态新增于WordPress 2.9。
- ‘post_parent’ => 93 —— 返回页面93的子页面。
置顶文章参数
置顶文章功能引入于WordPress 2.7。在查询中,被设为“置顶”的文章会显示在其它文章之前,除非该文章已经被caller_get_posts=1 参数排除。
- array(‘post__in’=>get_option(‘sticky_posts’)) —— 返回所有置顶文章的数组
- caller_get_posts=1 —— 排除返回的文章上方的置顶文章,但在返回文章列表时,以自然顺序将曾经置顶的文章安插在列表中。
返回第一篇置顶文章
$sticky=get_option('sticky_posts') ; query_posts('p=' . $sticky[0]);
或
$args = array( 'posts_per_page' => 1, 'post__in' => get_option('sticky_posts'), 'caller_get_posts' => 1 ); query_posts($args);
注意:第二种方法只能返回最新发表的置顶文章;若当前无置顶文章,返回最新发表文章。
返回第一篇置顶文章;若无,则不返回任何内容
$sticky = get_option('sticky_posts'); $args = array( 'posts_per_page' => 1, 'post__in' => $sticky, 'caller_get_posts' => 1 ); query_posts($args); if($sticky[0]) { // insert here your stuff... }
从查询中排除所有置顶文章
query_posts(array("post__not_in" =>get_option("sticky_posts")));
返回某一分类下所有文章,但不在文章列表上方显示置顶文章。所有设为“置顶”的文章以正常顺序(如日期顺序)显示
query_posts('caller_get_posts=1&posts_per_page=3&cat=6');
返回某一分类下所有文章,完全不显示置顶文章,保留分页
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $sticky=get_option('sticky_posts'); $args=array( 'cat'=>3, 'caller_get_posts'=>1, 'post__not_in' => $sticky, 'paged'=>$paged, ); query_posts($args); ?>
时间参数
检索特定时间段内发表的文章。
- hour= -hour (时,-范围从0到23)
- minute= – minute (分,-范围从0到60)
- second= – second (秒,-范围从0到60)
- day= – day of the month (日,-范围从1到31)
- monthnum= – month number (月,-范围从1到12)
- year= – 4 digit year (年,如2009)
- w= – week of the year(一年中的第几周,-范围从0到53),使用 MySQL WEEK command Mode=1命令
返回最近发表的文章
$today = getdate(); query_posts('year=' .$today["year"] .'&monthnum=' .$today["mon"] .'&day=' .$today["mday"] );
返回12月20日发表的文章
query_posts(monthnum=12&day=20' );
返回2009年3月1日到3月15日之间发表的文章
<?php //based on Austin Matzko's code from wp-hackers email list function filter_where($where = '') { //posts for March 1 to March 15, 2009 $where .= " AND post_date >= '2009-03-01' AND post_date < '2009-03-16'"; return $where; } add_filter('posts_where', 'filter_where'); query_posts($query_string); ?>
返回最近30天内发表的文章
<?php //based on Austin Matzko's code from wp-hackers email list function filter_where($where = '') { //posts in the last 30 days $where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'"; return $where; } add_filter('posts_where', 'filter_where'); query_posts($query_string); ?>
返回过去30天到过去60天内发表的文章
<?php //based on Austin Matzko's code from wp-hackers email list function filter_where($where = '') { //posts 30 to 60 days old $where .= " AND post_date >= '" . date('Y-m-d', strtotime('-60 days')) . "'" . " AND post_date <= '" . date('Y-m-d', strtotime('-30 days')) . "'"; return $where; } add_filter('posts_where', 'filter_where'); query_posts($query_string); ?>
分页参数
- paged=2 ——显示点击“较早的日志”链接后出现在第二页中的文章
- posts_per_page=10 —— 每页所显示的文章数量;若值为-1,显示所有文章。
- order=ASC —— 按时间顺序显示文章,若值为DESC则按逆向时间顺序显示文章(默认)
offset(偏移)参数
通过offset参数,你可以移除或忽略正常情况下被查询集中的一篇或多篇初始文章。
以下显示最近一篇文章之后的5篇文章:
query_posts('posts_per_page=5&offset=1');
排序参数
- orderby=author
- orderby=date
- orderby=category ——注意:该参数不能用于WordPress 2.8,可能已经被废止
- orderby=title
- orderby=modified
- orderby=menu_order
- orderby=parent
- orderby=ID
- orderby=rand
- orderby=meta_value —— meta_key=some value语句也应出现在查询参数中
- orderby=none – no order —— (新增于 WP 2.8)
- orderby=comment_count ——(新增于 WP 2.9)
顺序参数
决定以升序或降序排列排序参数
- order=ASC —— 升序,从最低值到最高值
- order=DESC —— 降序,从最高值到最低值
自定义字段参数
根据自定义关键字或值检索文章(或页面)。
- meta_key=
- metavalue=
- meta_compare= —— 用以测试metavalue=的操作符,默认值为 ‘=’,其它可能的值包括’!=’、 ‘>’、’>=’、 ‘<’或 ‘<=’ 。
返回关键字为 ‘color’ 且值为’blue’的文章:
query_posts('meta_key=color&metavalue=blue');
返回自定义字段关键字为’color’的文章,无论自定义字段值为何:
query_posts('meta_key=color');
返回自定义字段值为’color’的文章,无论关键字为何:
query_posts('metavalue=color');
返回自定义字段值为’green’的页面,无论自定义字段关键字为何:
query_posts('post_type=page&metavalue=green');
返回自定义关键字为’color’、自定义字段值不为’blue’的文章和页面:
query_posts('post_type=any&meta_key=color&meta_compare=!=&metavalue=blue');
返回自定义字段关键字为’miles’、自定义字段值小于等于22的文章。注意,字段值99会被看做大于字段值100,因为数据是以字符串形式而不是数字形式存储的。
query_posts('meta_key=miles&meta_compare=<=&metavalue=22');
联合参数
你可能已经从上面有些例子中看出来了,可以用&符号连接不同参数,如:
uery_posts('cat=3&year=2004');
显示主页上、当前月份发表的、隶属于分类13下的文章:
if (is_home()) { query_posts($query_string . '&cat=13&monthnum=' . date('n',current_time('timestamp'))); }
在WP 2.3中,以下参数联合会返回同时属于分类1和分类3的两篇文章,以文章标题降序排列:
query_posts(array('category__and'=>array(1,3),'posts_per_page'=>2,'orderby'=>title,'order'=>DESC));
在WP 2.3和WP 2.5中,以下参数联合本应返回属于分类1且带有“apples”标签的文章:
query_posts('cat=1&tag=apples');
但由于一个bug,代码没能显示出正常结果。有一个解决办法:利用+号查找多个标签:
query_posts('cat=1&tag=apples+apples');
这就显示出我们希望显示的结果了。
使用技巧
设置>阅读中的“博客页面最多显示”参数会影响你的查询结果,要覆盖设置>阅读中的设置,需要在标签中添加’posts_per_page’ 参数。例如:
query_posts('category_name=The Category Name&posts_per_page=-1'); //returns ALL from the category
注意:query_posts函数会改写并取代页面的主查询。为谨慎起见,请不要将query_posts用作其它用途。
来源: http://www.zuluo.net/2012/2012-01/wordpress-query_posts.html
发表评论
-
Magento: 后台显示图片不能找到 Image file was not found on product tab
2016-08-30 02:22 3005I was uploading some images f ... -
理解WordPress的PingBack和TrackBack
2016-08-26 02:21 5811pingback和trackback的功 ... -
零基础 Amazon Web Services (AWS) 入门教程图文版(四)
2016-06-07 11:40 1036自上一篇之后,5天过去了,这篇文章总算是挤出来了... 其实 ... -
零基础 Amazon Web Services (AWS) 入门教程图文版(三)
2016-06-07 11:41 2063原则上WDCP安装好了,就可以直接使用了,FTP、MySQL ... -
零基础 Amazon Web Services (AWS) 入门教程图文版(二)
2016-06-07 01:33 1021上一篇讲到,主机正常运转了。但是此时如果直接访问公网IP是 ... -
零基础 Amazon Web Services (AWS) 入门教程图文版(一)
2016-06-07 01:31 21807现在小站唯一的流量都靠AWS这个关键词了,刚好要用AWS重新 ... -
零基础 Amazon Web Services (AWS) 入门教程 (列表)
2016-06-07 01:32 2099在 Amazon Web Services 上托 ... -
Magento 1.9:新订单通知 Admin Order Notifier
2016-06-02 02:48 870Here is a little Mag ... -
Magento 1.9.X 系列教程
2016-05-14 02:44 2305Magento安装下载教学: Magento教程 1 ... -
Magento: 产品页面下jquery change函数失效 Call javascript function onchange product option
2016-05-05 06:39 1775明显的原因是change函数跟magento默认的oncha ... -
Magento: 判断是否为手机浏览 Optimise Web's Mobile Detect Class for Magento
2016-04-29 07:01 1178项目地址:Optimise Web's Mobile Det ... -
Magento: Gird 和 form 区域 Module Development Series – Magento Admin Module
2016-04-28 02:39 690In this tutorial, we are goi ... -
Magento: addAttributeToFilter 和 addFieldToFilter 的区别 Difference between addAttri
2016-04-28 02:34 1027addAttributeToFilter is use ... -
Magento: 后台获取menu链接 Getting the admin panel urls
2016-04-28 02:34 791The url for customer page in t ... -
Magento : 调用文件上传 upload file frontend
2016-04-27 01:25 1377bool mkdir ( string $pathname ... -
Magento: 自定义用户登录导向页面 Redirect Customer to Previous Page After Login
2016-04-26 02:45 1869Configuration Settings – L ... -
Magento: 代替flash上传 How to disable Flash uploader in Magento (product images and
2016-04-06 05:04 11401. 替换产品页flash上传按钮 - 使用 Du ... -
Magento: 在客户账户中添加自定义链接 My Account Add Link
2016-04-05 14:05 1420This extension add new link an ... -
Magento: 根据产品属性加载产品信息 Load A Category or Product by an Attribute
2016-03-26 01:35 961Load a Product by ID <?ph ... -
Magento模块开发之数据库SQL操作方法说明
2016-03-26 01:31 1394今天主要来看Magento中的Mysql4/Resource ...
相关推荐
在WordPress中,`query_posts()` 是一个非常重要的函数,它允许开发者在页面内灵活地调用和展示特定条件下的文章。这个函数可以帮助我们创建自定义的循环,展示来自不同分类、标签、日期或其他条件的文章列表,从而...
做过wordpress模版开发的都知道query_post()函数是wordpress最常用的函数之一,比如读取最新文章,读取指定分类文章,读取指定标签文章等等都用到了query_post()函数。下面让我们来看几个关于query_post()的实际应用...
同时,为了优化性能,可以考虑使用`WP_Query`替代`query_posts`,以减少对主查询的影响。 总结来说,虽然WordPress插件提供了一键配置的相关文章功能,但它们可能影响网站速度。通过代码实现相关文章,虽然配置较为...
结合其他WordPress函数,如文章循环(The Loop)、文章查询(WP_Query)以及文章元数据(post meta)操作,你将能够构建出强大的统计功能,满足各种定制需求。 总之,`wp_count_posts()` 是WordPress中的一个强大...
在WordPress开发中,了解和熟练使用`get_post`和`get_posts`这两个函数至关重要。它们是WordPress内置的函数,用于处理和获取文章数据。本文将详细解析这两个函数的用法和特点。 首先,`get_post()`函数通常在开发...
除了基本的主循环,还可以自定义查询循环,比如使用`new WP_Query`创建新的查询对象,或者使用`query_posts`进行页面内查询。但需要注意,`query_posts`不推荐在主题中广泛使用,因为它会影响全局查询状态。`WP_...
WordPress 调用函数大全 WordPress 是一个功能强大且广泛使用的内容管理系统,具有非常多的函数调用方法。在本文中,我们将对 WordPress 中的一些常用的函数调用方法进行汇总,旨在帮助大家更好地掌握 WordPress ...
5. **使用WP_Query的参数**:正确使用WP_Query的参数,如`posts_per_page`,以避免额外的分页查询。 6. **避免在循环中查询**:将查询移到循环外,避免每次迭代都执行查询。 通过以上方法,你可以有效地减少...
$query_posts->query($args); while ($query_posts->have_posts()) { $query_posts->the_post(); ?> <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li> ...
3. `'caller_get_posts' => 1`:这个参数是为了防止WordPress自动合并当前查询与主查询,确保只显示置顶文章。 需要注意的是,如果没有足够的置顶文章满足`posts_per_page`设置的数量,`query_posts`不会自动填充...
#### 三、WordPress函数技巧 - **Loop循环(成功)**:使用 `( have_posts() ) : while ( have_posts() ) : the_post(); ?>` 来遍历文章。 - **在WordPress的第一篇文章中插入Google广告(成功)**:可以在单篇文章...
WordPress是目前全球范围内使用最广泛的网站构建和内容管理系统之一,它拥有庞大的用户群体和丰富的插件资源,用户可以通过WordPress后台轻松管理和发布内容。WordPress的核心代码由PHP语言编写,同时支持使用模板...
- **WordPress函数技巧**:包括使用`Loop`循环来遍历Post、在文章中插入广告、改变每个分类页面的显示数目、为特定分类添加不同模板等。 - **样式化不同分类**:通过CSS类或ID,可以为不同分类的文章应用不同的样式...
- 使用`query_posts()`函数可以定制文章查询,例如按特定分类、作者或关键词筛选文章。 - 结合`pre_get_posts`动作钩子,可以在不修改默认查询的情况下,动态地调整查询参数。 - `get_posts()`函数可以获取一组文章...
本文将深入探讨WordPress文章查询的循环Loop结构及其使用方法,并简要介绍WP_Query类的基本功能。 WordPress的循环(Loop)是用于遍历和显示文章的一种机制。最基本的循环结构由`have_posts()`和`the_post()`这两个...
($query_posts->have_posts()) : $query_posts->the_post(); ?> <li><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> ; ?> ``` ...
此外,WordPress还提供了许多其他模板标签和函数,如自定义URL结构`/%postname%/`,以及`the_search_query()`用于显示当前的搜索查询等。这些函数和标签构成了WordPress模板系统的基础,使得开发者能够灵活地控制...
虽然在这个例子中它被用来修改主查询,但在某些情况下,过度使用query_posts可能会导致性能问题,因为它会重置全局查询变量。更好的做法是使用pre_get_posts钩子来修改查询,这是在WordPress中处理查询修改的推荐...
这个函数允许我们对主查询进行自定义,从而获取不同的文章列表。以下是一个调用最近10篇文章的示例: ```php query_posts('showposts=10&orderby=new'); // showposts=10 表示显示10篇文章,orderby=new 表示按...