填写您的邮件地址,订阅我们的精彩内容:

关注wordpress文章Loop循环中的$wp_query

593

在wordpress主题制作过程中,你可能会忽视LOOP循环中的全局参数$wp_query,它对于一些特殊情况下的开发有着非常重要的作用。我们可以利用$wp_query获取很多相关的信息,从而实现文章之外的查询和记录调用。

$wp_query记录的是对数据库进行一次检索得到的结构,它可以在query_posts()后面被打印,也可以在while(have_posts()):the_post();endwhile;中被打印,它的一些键值有着非常有效的作用。

举一个很简单的例子:我们可以利用它来获取在LOOP中当前显示的是第几篇文章,我们只需要用$current_post = $wp_query->current_post;即可获得(注意,$current_post的值从0开始自加1)。

我们也可以用$current_page = $wp_query->query_vars[‘paged’];得到当前是第几页。

我们可以在the_post()后使用print_r($wp_query)了解当前的$wp_query值。

它的基本构成类似是:(下面是我放在the_post()后的效果)

[code lang=”php” line=”1″]
WP_Query Object
(
[query_vars] => Array
(
[cat] => -94,-117
[paged] => 1
[posts_per_page] => 2
[post__in] => Array
(
[0] => 1352
)

[caller_get_posts] => 1
[orderby] => modified
[error] =>
[m] => 0
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[static] =>
[pagename] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[category_name] =>
[tag] =>
[tag_id] =>
[author_name] =>
[feed] =>
[tb] =>
[comments_popup] =>
[meta_key] =>
[meta_value] =>
[preview] =>
[s] =>
[sentence] =>
[fields] =>
[category__in] => Array
(
)

[category__not_in] => Array
(
[0] => 94
[1] => 117
)

[category__and] => Array
(
)

[post__not_in] => Array
(
)

[tag__in] => Array
(
)

[tag__not_in] => Array
(
)

[tag__and] => Array
(
)

[tag_slug__in] => Array
(
)

[tag_slug__and] => Array
(
)

[ignore_sticky_posts] => 1
[suppress_filters] =>
[cache_results] => 1
[update_post_term_cache] => 1
[update_post_meta_cache] => 1
[post_type] =>
[nopaging] =>
[comments_per_page] => 50
[no_found_rows] =>
[order] => DESC
)

[tax_query] => WP_Tax_Query Object
(
[queries] => Array
(
[0] => Array
(
[taxonomy] => category
[terms] => Array
(
[0] => 94
[1] => 117
)

[include_children] =>
[field] => term_id
[operator] => NOT IN
)

)

[relation] => AND
)

[meta_query] => WP_Meta_Query Object
(
[queries] => Array
(
)

[relation] =>
)

[post_count] => 1
[current_post] => 0
[in_the_loop] => 1
[comment_count] => 0
[current_comment] => -1
[found_posts] => 1
[max_num_pages] => 1
[max_num_comment_pages] => 0
[is_single] =>
[is_preview] =>
[is_page] =>
[is_archive] =>
[is_date] =>
[is_year] =>
[is_month] =>
[is_day] =>
[is_time] =>
[is_author] =>
[is_category] =>
[is_tag] =>
[is_tax] =>
[is_search] =>
[is_feed] =>
[is_comment_feed] =>
[is_trackback] =>
[is_home] => 1
[is_404] =>
[is_comments_popup] =>
[is_paged] =>
[is_admin] =>
[is_attachment] =>
[is_singular] =>
[is_robots] =>
[is_posts_page] =>
[is_post_type_archive] =>
[query_vars_hash] => 062525d5faf49068e8e7abb314f2d7b6
[query_vars_changed] =>
[thumbnails_cached] =>
[query] => Array
(
[cat] => -94,-117
[paged] => 1
[posts_per_page] => 2
[post__in] => Array
(
[0] => 1352
)

[caller_get_posts] => 1
[orderby] => modified
)

[request] => SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.ID IN (1352) AND wp_posts.post_type = ‘post’ AND (wp_posts.post_status = ‘publish’) GROUP BY wp_posts.ID ORDER BY wp_posts.post_modified DESC LIMIT 0, 2
[posts] => Array
(
[0] => stdClass Object
(
[ID] => 1352
[post_author] => 1
[post_date] => 2012-08-22 22:09:32
[post_date_gmt] => 2012-08-22 14:09:32
[post_content] =>
[post_title] =>
[post_status] => publish
[comment_status] => open
[ping_status] => open
[post_password] =>
[post_name] => %e6%b7%bb%e5%8a%a0%e4%b8%80%e4%b8%aa%e6%a0%87%e7%ad%be%e8%af%95%e8%af%95
[to_ping] =>
[pinged] =>
[post_modified] => 2012-08-27 16:09:52
[post_modified_gmt] => 2012-08-27 08:09:52
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://localhost/theme/?p=1352
[menu_order] => 0
[post_type] => post
[post_mime_type] =>
[comment_count] => 10
[filter] => raw
)

)

[post] => stdClass Object
(
[ID] => 1352
[post_author] => 1
[post_date] => 2012-08-22 22:09:32
[post_date_gmt] => 2012-08-22 14:09:32
[post_content] =>
[post_status] => publish
[comment_status] => open
[ping_status] => open
[post_password] =>
[post_name] => %e6%b7%bb%e5%8a%a0%e4%b8%80%e4%b8%aa%e6%a0%87%e7%ad%be%e8%af%95%e8%af%95
[to_ping] =>
[pinged] =>
[post_modified] => 2012-08-27 16:09:52
[post_modified_gmt] => 2012-08-27 08:09:52
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://localhost/theme/?p=1352
[menu_order] => 0
[post_type] => post
[post_mime_type] =>
[comment_count] => 10
[filter] => raw
)

)
[/code]

下面我简单说几句